Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11382 | Accepted: 4882 |
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
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
Sample Input
25 5 2 2 14 11 21 17
Sample Output
4
Hint
题意:一群牛要过河,河中有许多石墩,FJ为了锻炼牛的跳跃力,将其中的m个石墩摘掉,求所有情况中最大的(跳跃最小值)。
思路:用一个变量sum累加距离,最小跳跃值mid通过二分来枚举。
(1)如果需要摘除的石墩超过m的话,说明牛的跳跃值太大了,并不是最小跳跃值。
(2)如果可以摘除的石墩刚好等于m的话,也不一定是最小的。
(3)如果小于m的话,说明mid偏小。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int inf=0x3f3f3f;
const int MAXN=50000+10;
int a[MAXN],dis[MAXN];
int l,n,m;
int high,low;
bool judge(int mid)
{
int i,sum=0,cnt=0;
for(i=1;i<=n+1;++i)
{
if(sum+dis[i]<=mid)
{
sum+=dis[i];
cnt++;
}
else sum=0;
}
if(cnt<=m)return 1;
else return 0;
}
void solve()
{
int mid;
while(low<=high)
{
mid=(high+low)>>1;
if(judge(mid))
{
low=mid+1;
}
else high=mid-1;
}
printf("%d\n",low);//这种题,需要结合示例看看返回mid还是low还是high(QAQ不是很懂,关键还得看数据)
}
int main()
{
int i;
scanf("%d%d%d",&l,&n,&m);
a[n+1]=l;
for(i=1;i<=n;++i)scanf("%d",&a[i]);
sort(a+1,a+1+n);
high=l;low=inf;
for(i=1;i<=n+1;++i)
{
dis[i]=a[i]-a[i-1];
low=min(low,dis[i]);
}
solve();
return 0;
}