bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
来源:互联网 发布:程序员必备的手机app 编辑:程序博客网 时间:2024/06/14 00:12
题意
点这里
什么?你说这是英文你不想看?
那我也不告诉你
解法
这题其实我看了官方题解。。
也是英文的。。
就是将奶牛的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;}
阅读全文
0 0
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- BZOJ 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II LCS
- BZOJ 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 树状数组优化LCS
- BZOJ 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 逆序对
- 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- 【HTML5】<input>的日期类型Date
- AndrodStudio:Enable ADB integration' to be enabled
- Docker常用命令和操作
- js 去掉字符串左右的空格
- linux常用命令
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- ReactNative——键盘遮挡问题
- 索引堆
- 菜鸟面试必知的 http 知识(一)—— TCP握手协议
- microsoft sql server management studio 弹出 字体“Aharoni” 不支持“Regular”
- java.net.SocketException: Software caused connection abort: socket write error
- 将字符串内的特定的字符或字符串替换成另一个字符或字符串(纯C)
- Java I/O操作
- 微信OAuth2网页授权登陆接口