POJ 2456 (二分查找)
来源:互联网 发布:犀牛软件建模教程 编辑:程序博客网 时间:2024/06/03 17:03
题意:
将m头牛放到n个牛棚中,如何使它们的间距最大。每个牛棚有对应的位置。
思路:二分法总能够暴力查找出答案,注意点就是如何check,这个需要一定的仔细。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;typedef long long LL;LL a[100005];int n,c;int check(int d){ int last = 0; for(int i = 1;i < c; i++) { int cnt = last + 1; while(cnt < n && a[cnt] - a[last] < d) cnt++; if(cnt == n) return false; last = cnt; } return true;}int main(){ //freopen("in.txt","r",stdin); scanf("%d%d",&n,&c); for(int i = 0;i < n; i++) scanf("%lld",&a[i]); sort(a,a+n); int l = 0,r = INF; while(r - l > 1) { int mid = (l + r)/2; if(check(mid)) l = mid; else r = mid; } printf("%d\n",l); return 0;}
0 0
- POJ 2456 (二分查找)
- 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 3670 (LIS+二分查找)
- POJ 3273 Monthly Expense(二分查找)
- POJ 2503 Babelfish(二分查找)
- POJ 3663 Costume Party (二分查找)
- poj 3104 Drying(二分查找)
- poj--2549--Sumsets(二分查找)
- POJ 2002 Squares(hash+二分查找)
- JVM结构探究
- leetcode
- 第2条:遇到多个构造器参数时要考虑用构建器
- 树的前序遍历(递归和非递归java实现)
- 不允许创建临时变量,交换两个数的内容||求10个整数中最大值||求一个函数返回参数二进制中1的个数
- POJ 2456 (二分查找)
- Consul 简介、安装、常用命令的使用
- 第3条:用私有构造器或者枚举类型强化Singleton属性
- leetcode
- Kotlin enum类与companion设计的不协调--NPE导致ClassNotFoundException, Could not initialize class.
- 使用Visual Studio 2017作为Linux C++开发工具
- 没有p sensor时要求电话接通5s灭屏
- 【图像处理】CanonVRD Tags
- gulp-load-plugins[模块化管理插件]