POJ river hopscotch
来源:互联网 发布:万户网络怎么样 编辑:程序博客网 时间:2024/04/28 01:35
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Line 1: Three space-separated integers: L, N, and M
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
二分,神奇的特例。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
long long l,n,m;
long long a[50005];
long long sum;
int main()
{
int i,j;
while(~scanf("%lld%lld%lld",&l,&n,&m))
{
memset(a,0,sizeof(a));
a[n+1]=l;//将河岸看做两块石头
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
sort(a,a+n+1);
long long mid;
long long low=0,high=l;
if(n==m) cout<<l<<endl;
else
{
while(1)
{
if(mid==(low+high)/2) break;
int cnt=0;
mid=(low+high)/2;
for(i=1,j=0;i<=n+1;)
{
if(a[i]-a[j]<mid)
{
cnt++;
i++;
}
else
{
j=i;
i++;
}
}
if(cnt>m)//不符合条件,mid过大
high=mid;
else
low=mid;//该mid值较小,非最优解,但符合条件
}
cout<<low<<endl;
}
}
return 0;
}
- poj 3258 River Hopscotch
- Dichotomy poj River Hopscotch
- poj-3258 River Hopscotch
- poj-3258 River Hopscotch
- poj 3258 River Hopscotch
- poj 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- poj 3258 River Hopscotch
- POJ-3258-River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- poj 3258-River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258 River Hopscotch
- POJ 3258--River Hopscotch
- OFDM总结
- 8.17 想越狱的小杉 1535
- Nginx 实现虚拟机
- selenium2.0如何解决对于IE11的加载支持
- nginx随系统启动
- POJ river hopscotch
- HDU 6138 Fleet of the Eternal Throne AC自动机||后缀数组
- 美国为什么不禁枪?
- 集训8.17
- POJ
- OJ -338 Counting bits
- 2017年8月15日提高组T3 电影
- HDU 1285 确定比赛名次
- 网易疯狂队列