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 enanswerng rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral answerstance answer from the start (0 < answer < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the enanswerng rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, enanswerng 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 answerstances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest answerstance a cow will have to jump to reach the end. He knows he cannot remove the starting and enanswerng rocks, but he calculates that he has enough resources to remove up toM rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest answerstance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest answerstance a cow has to jump after removing the optimal set of M rocks.
Input
Lines 2.. N+1: Each line contains a single integer inanswercating 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
题意:
一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L。河中有n块石头,每块石头到S都有唯一的距离问现在要移除m块石头(S和E除外),要求移除m块石头后,使得那时的最短距离尽可能大,输出那个最短距离。
---------------------
作者:Calm微笑
来源:CSDN
原文:https://blog.csdn.net/yao1373446012/article/details/51025699
版权声明:本文为博主原创文章,转载请附上博文链接!
解题思路:
由题意易知,answer一定在区间【0,L】,
那么我们可以二分查找answer,以为answer间隔依次挑选石头,数量记为num,
如果num<=m,说明answer选小了,此时令low=mid+1; answer=mid;(answer=mid,是因为间距小于mid的数量num要小于m,
也就是说如果此时的answer为最终答案,那么一定要移动的石头数量num小于m,剩余的移动次数可去移动别的,不影响此时的结果,所以令answer=mid
如果num>m,说明answer选大了;此时令high=mid-1;
#include<stdio.h> #include<algorithm> using namespace std; int n; int main() { int l,m,i,ans; int dist[50000+5]; while(~scanf("%d%d%d",&l,&n,&m)) { dist[0]=0; dist[n+1]=l; for(i=1;i<=n;i++) scanf("%d",dist+i); sort(dist+1,dist+n+1); int low=0,high=l; while(high-low>=0) { int num=0,present=0,mid=(low+high)>>1,sum=0; for(i=1;i<=n+1;i++) if((sum+=dist[i]-dist[i-1])<mid)//两相邻石头间距比 预定最大最小间距mid小,就移除一次石头 num++; else sum=0; if(num<=m)//移动的次数少于m,即间距小于mid的石头少于m,说明mid要变大一点 { low=mid+1; ans=mid; } else high=mid-1; } printf("%d ",ans); } return 0; }