poj 2456 Aggressive cows
来源:互联网 发布:od矩阵模型 编辑:程序博客网 时间:2024/06/07 20:43
(1)类型:二分搜索+贪心
(2)题意:农夫约翰搭了一间有N间牛舍的小屋,牛舍排在一条线上,第i号牛舍在X(i)在X(i)的位置。但是他的M头牛对小屋很不满意,因此经常互相攻击。约翰为了防止牛之间互相伤害,因此决定把每头牛都放在离其他牛尽可能远的牛舍。也就是要最大化最近的两头牛之间的距离。
(3)解题思路:类似的最大化最小值或者最小化最大值的问题,通常用二分搜索法就可以很好的解决。本题目中,在间距问题上还要借助贪心算法。
(4)时间复杂度:快排:O(nlog(n)),贪心算法中对牛舍的处理:O(n);总的时间复杂度属于O(nlog(n))级别。
(5)总结:老师刚刚说了Java工程师需要学习的东西,可是我现在只想搞acm,我不想放弃,也不会放弃。因为还没有结束。
(6)代码:
#include<iostream>#include<algorithm>const int INF=0x3f3f3f3f;using namespace std;const int MAX_N=1e5+6;int n,m,x[MAX_N];bool C(int d){ int last=0; for(int i=1;i<m;i++){ int crt=last+1; while(crt<n&&x[crt]-x[last]<d){ crt++; } if(crt==n)return false; last=crt; } return true;}void solve(){ sort(x,x+n); int lb=0,ub=INF; while(ub-lb>1){ int mid=(lb+ub)/2; if(C(mid))lb=mid; else ub=mid; } cout<<lb<<endl;}int main(){ cin>>n>>m; for(int i=0;i<n;i++)cin>>x[i]; solve(); }
阅读全文
1 0
- Poj 2456 Aggressive cows
- POJ 2456 Aggressive cows
- poj 2456 Aggressive cows
- poj 2456 Aggressive cows
- POJ-2456-Aggressive cows
- POJ 2456 Aggressive cows
- POJ-2456 Aggressive cows
- poj 2456 Aggressive cows
- POJ 2456 Aggressive cows
- POJ 2456 Aggressive cows
- poj 2456 Aggressive cows
- POJ 2456 Aggressive cows
- POJ 2456 Aggressive cows
- POJ-2456 Aggressive cows
- POJ-2456Aggressive cows
- POJ 2456 - Aggressive cows
- POJ 2456 Aggressive cows
- poj 2456 Aggressive cows
- Mybatis-generator
- spring boot与spring mvc的区别是什么?
- 1118: 数列有序
- 为什么有这么多 Python?Python是解释型的还是编译型的?
- OpenStack云环境数据备份方案 Freezer
- poj 2456 Aggressive cows
- 计算机网络自顶向下方法第二章笔记
- Hiho_1015:KMP算法
- epel
- 5.2
- 一张细化了的hbase存储结构图
- (一)数据结构与算法--前篇
- STL之list容器详解
- 价值200元的小程序卡卷开发免费送