水题,20分钟AC,最大值最小,一看就是二分答案。。。
代码:
Description Farmer John has built a new long barn, with N ( <= N <= ,) stalls. The stalls are located along a straight line at positions x1,...,xN ( <= xi <= ,,,). His C ( <= 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? 农夫 John 建造了一座很长的畜栏,它包括NN ( <= N <= ,)个隔间,这些小隔间依次编号为x1,...,xN ( <= xi <= ,,,). 但是,John的C ( <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢
Input
* Line : Two space-separated integers: N and C * Lines ..N+: Line i+ contains an integer stall location, xi
第一行:空格分隔的两个整数N和C
第二行---第N+1行:i+1行指出了xi的位置
Output
* Line : One integer: the largest minimum distance
第一行:一个整数,最大的最小值
Sample Input Sample Output 把牛放在1,,8这样最小距离是3
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const long long INF = (60LL);
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int l,r,maxn = ,n,c,ok;
ll x[];
int main()
{
read(n);
read(c);
duke(i,,n)
{
read(x[i]);
if(x[i] > maxn)
{
maxn = x[i];
}
}
sort(x + ,x + n + );
l = ;
r = maxn;
while(l != r)
{
ok = ;
int mid = (l + r) >> ;
int tot = ,ans = ;
// cout<<endl;
// cout<<mid<<endl;
duke(i,,n)
{
if(x[i] - x[i - ] + tot < mid)
{
tot = tot + x[i] - x[i - ];
}
else
{
// cout<<tot<<" "<<x[i] - x[i - 1]<<endl;
ans ++;
tot = ;
}
if(ans >= c)
{
ok = ;
break;
}
}
if(ok == )
{
l = mid + ;
}
else
{
r = mid;
}
}
ok = ;
int mid = l;
int tot = ,ans = ;
duke(i,,n)
{
if(x[i] - x[i - ] + tot < mid)
{
tot = tot + x[i] - x[i - ];
}
else
{
ans ++;
tot = ;
}
if(ans >= c)
{
ok = ;
break;
}
}
if(ok == )
write(l);
else
write(l - );
return ;
}
/*
7 3
1
2
3
7
8
9
10
*/