当前位置: 代码迷 >> 综合 >> bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
  详细解决方案

bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road

热度:34   发布时间:2023-10-29 12:27:14.0

题意

点这里
什么?你说这是英文你不想看?
那我也不告诉你

解法

这题其实我看了官方题解。。
也是英文的。。
就是将奶牛的r为第一关键字,l为第二关键字,从小到大排序
然后将奶牛扫过去
对于每一头奶牛,就找一只剩下的与他l节点最接近的鸡和他尝试配对
配对成功了,就把鸡删掉,然后ans++
这些都可以用set在logn时间做完
不会set的可以看这里点这里
为什么这样是最优的呢?
我们可以分类讨论一下
这里写图片描述
首先对于两条线段,情况就只有上面两种。。
不想交的就没有画了,显然我选什么和你没有关系
看看图就知道了。。然后我们可以发现,上面的线段只要选择靠近他左端点的,答案都不会差
正确性显然。。

感觉这个贪心题没做出来,真的是最近变水了QAQ

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;
const int N=20005;
int c,n;
multiset<int> s;
multiset<int>::iterator it;
struct qq
{int l,r;
}cow[N];
bool cmp (qq a,qq b){
   return a.r==b.r?a.l<b.l:a.r<b.r;}
int main()
{scanf("%d%d",&c,&n);for (int u=1;u<=c;u++){int x;scanf("%d",&x);s.insert(x);}for (int u=1;u<=n;u++)  scanf("%d%d",&cow[u].l,&cow[u].r);sort(cow+1,cow+1+n,cmp);int ans=0;for (int u=1;u<=n;u++){it=s.lower_bound(cow[u].l);if (it!=s.end()&&*it<=cow[u].r){ans++;s.erase(it);}}printf("%d\n",ans);return 0;
}
  相关解决方案