Day3-R-Aggressive cows POJ2456
His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
Input
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
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.
const int maxm = ;
int buf[maxm], n, k;
bool check(int d) {
int t = , now = buf[];
for (int i = ; i < n; ++i) {
if(buf[i] - now >= d) {
now = buf[i];
t++;
}
if(t >= k)
return true;
}
return false;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = ; i < n; ++i)
scanf("%d", &buf[i]);
sort(buf, buf + n);
int l = , r = buf[n - ] - buf[] + , mid;
while(l < r) {
mid = (l + r) >> ;
if(check(mid))
l = mid + ;
else
r = mid;
}
printf("%d\n", l - );
return ;
}
(左闭右闭)
const int maxm = ;
int buf[maxm], n, k;
bool check(int d) {
int t = , now = buf[];
for (int i = ; i < n; ++i) {
if(buf[i] - now >= d) {
now = buf[i];
t++;
}
if(t >= k)
return true;
}
return false;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = ; i < n; ++i)
scanf("%d", &buf[i]);
sort(buf, buf + n);
int l = , r = buf[n - ] - buf[], mid;
while(l <= r) {
mid = (l + r) >> ;
if(check(mid))
l = mid + ;
else
r = mid - ;
}
printf("%d\n", r);
return ;
}
小结:对于问题可进行贪心验证+答案具有单调性可以进行二分,注意左闭右闭的while条件
Day3-R-Aggressive cows POJ2456的更多相关文章
- POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13993 Accepted: 6775 ...
- 二分法的应用:最大化最小值 POJ2456 Aggressive cows
/* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- POJ2456 Aggressive cows
Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...
- 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...
- 【POJ - 2456】Aggressive cows(二分)
Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11192 Accepted: 5492 ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- 疯牛-- Aggressive cows (二分)
疯牛 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...
- 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 217 Solved: ...
随机推荐
- [LeetCode] 735. Asteroid Collision
行星碰撞. 题意是给一个数组 asteroids,表示在同一行的行星.对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相同的速度移 ...
- JPA 级联保存的问题
前提:系统有学校-学生关系,学校可以包含多个学生,学生只能属于一个学校 在使用 spring-data-jpa 的时候,保存学校的同时保存学生信息,不需要先逐个保存学生信息,再将学生信息放在学校中保存 ...
- Codeforces Round #592 (Div. 2)G(模拟)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[1000007],b[ ...
- video标签 在微信浏览器打开,不弹出大的独立窗口 而是直接播放。
1.在 video 标签中添加 属性 x5-playsinline playsinline webkit-playsinline="true" 2.ckplayer的 ...
- requests 用法小速记
Request库安装方法 Request官网 使用管理员权限启动 command控制台(win+X 命令提示符(管理员)) 使用pip安装requests库(默认配置好python以及pip的环境变量 ...
- mssql-osql
mssql导入单行字段值非常长,或者sql文件非常大,比如上百M或者更大,常规方法是导不进去的,所以推荐下面方式进行导入. osql -S . -U sa -P 123456 -d TS_TEST - ...
- Swift3.0-基本运算符
一.简介 运算符是检查.改变.合并值的特殊符号或者短语.在本篇文章中只介绍基本运算符,Swift中包含的高级运算符(比如溢出运算符)不在其中.Swift中的运算符和OC中的运算法还是有比较大的区别的, ...
- java课后问题解答
1.当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机 答:当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位 置抛出,可能会导致不同的finall ...
- rc
1,协同过滤. 2,协方差:用来衡量,他们的变化趋势是不是一致的. 3,皮尔逊相关系数:-1,负相关.1:正相关. 4,用皮尔逊相关系数来算相关性是最多的.
- 二 Spring的IOC入门,环境搭建,Spring测试类
IOC:inversion of Control 控制反转,Spring框架的核心.削减计算机程序的耦合问题,把对象(例如JDBC)的创建权交给Spring. IOC的两种类型: 依赖注入: 依赖查 ...