POJ 2456 Aggressive cows (二分查找)
来源:互联网 发布:java api 1.7中文版 编辑:程序博客网 时间:2024/05/20 03:07
Description
给n个牛栏坐标和c头牛,把c个牛放到n个牛栏上,求满足任意两头牛位置最远的最小值
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
Soulution
二分法最小化最大值,实现一遍check函数,例如对于数列1 2 4 8 9
,求距离是0~10时,分别能放几头牛,距离是1时能放5头牛,如果大于所给的牛,则距离1太小,需要增加距离即取1到10的mid。
#include <iostream>#include <cmath>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int INF = 1000000000;const int maxn = 110000;int N, C;int x[maxn];bool Check(int mid){ int num = 1, cur = 0, last = 0, i = 0; while (i < N) { if (x[cur] - x[last] >= mid) { num++; last = cur; } i++; cur = i; } if (num >= C) { return false; } else { return true; }}int main(){ while (~scanf("%d%d", &N, &C)) { memset(x, 0, sizeof(x)); for (int i = 0; i < N; i++) { scanf("%d", &x[i]); } sort(x, x + N); int l = 0, r = INF; for (int t = 0; t < 100; t++) { int mid = l + (r - l) / 2; if (Check(mid)) { r = mid; } else { l = mid; } } cout << l << "\n"; } return 0;}
阅读全文
0 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 二分查找
- [ACM] 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 (贪心 + 二分)
- MySQL 设置远程登录
- 全志R16平台点亮GC0308(分色排版)V1.0
- 地铁车厢里手机观看电视频道直播的解决方案
- Linux下快速搭建FTP服务
- android 7.0 加载system.loadLibrary崩溃事件解决--.so文件缺失
- POJ 2456 Aggressive cows (二分查找)
- 使用subline build c++
- VS代码提示快捷键
- 粗略计算两个 NSDate之间相差几天
- python 不同版本下载资源
- Spring+SpringMVC+Mybatis整合--第一天
- Gym 101190E (NEERC2016) Expect to Wait
- Swift iOS 8调用系统相机的拍照
- Java中static 和final的区别