poj Aggressive cows
来源:互联网 发布:淘宝模板什么意思 编辑:程序博客网 时间:2024/06/06 01:53
Description
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 312849
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.
这道题要注意二分搜索的终止条件,主要还是贪心思想
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010000;
int a[N];
int n, c;
int judge(int k);
int main()
{
scanf("%d %d", &n, &c);
int dis=-1;
for(int i=0; i<n; i++)
{
scanf("%d", &a[i]);
dis=max(dis,a[i]);
}
sort(a,a+n);
int l=0, r=dis+100000, mid;
while(r-l>1)
{
mid=(l+r)/2;
if(judge(mid))
{
l=mid;
}
else
{
r=mid;
}
}
printf("%d\n",l);
return 0;
}
int judge(int k)
{
int last=0;
for(int i=1;i<c;i++)
{
int flag=last+1;
while(flag<n&&(a[flag]-a[last]<k))
{
flag++;
}
last=flag;
if(flag==n)
{
return 0;
}
}
return 1;
}
- 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
- hdoj 4548 美素数
- 设计模式之代理模式
- Scalaz(49)- scalaz-stream: 深入了解-Sink/Channel
- android沉浸式
- jQuery Mobile 工具栏
- poj Aggressive cows
- HDOJ 4548 美素数
- C学习笔记(十二)函数详解
- 旧识——快速傅里叶变换
- 【Codeforces666B】【World Tour】【最短路】
- 从零开始搭建web项目并部署到本地服务器
- 成为一个开发者
- [ARM]ldr和 adr/adrl 伪指令的区别
- HDOJ 1084 What Is Your Grade?