标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10842 | Accepted: 4654 |
Description
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
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
Output
Sample Input
25 5 2 2 14 11 21 17
Sample Output
4
题目大意:奶牛过河游戏,胆小的奶牛只敢跳往最近的石头,河长L米,里面有N块石头,给你每一块石头距离起始位置的距离,
现在让你移除M块石头,使得相邻石头之间的距离最大,让你输出最长的距离是多少。
思路分析:最近几天一直在做二分的的题目,导致做这道题的时候读懂题意,看了下数据范围就基本确定是用二分来写了,基本
都差不多,但是每一道题都有需要值得注意的地方,比如为了防止溢出特地用了__int64(不造有没有用),本题弱还是贡献了
两发wa,原因主要是在起点和终点的处理上,起点和终点的石头是不能移动的!后来把终点单独拿出来进行处理,其余的如果不满足
就去掉右边的石头,成功A掉。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=50000+100;
__int64 a[maxn];
int n,m;
bool check(__int64 x)
{
int t=0;
int i;
__int64 len;
__int64 now=a[0],next;
__int64 sum=0;
for(i=n;i>=0;i--)
{
if(a[n+1]-a[i]<x) t++;
else break;
}
int h=i;
for(i=1;i<=h;i++)
{
next=a[i];
len=next-now;
if(len<x)
{
t++;
if(t>m) return false;
}
else now=a[i];
}
return true;
}
int main()
{
__int64 L;
while(scanf("%I64d%d%d",&L,&n,&m)!=EOF)
{
a[0]=0;
for(int i=1;i<=n;i++)
scanf("%I64d",&a[i]);
a[n+1]=L;
sort(a,a+n+2);
__int64 l=0,r=L;
__int64 ans=0;
while(l<=r)
{
__int64 mid=(l+r)>>1;
//cout<<mid<<endl;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
//cout<<check(4)<<endl;
printf("%I64d\n",ans);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/xuejianye/p/5517807.html