当前位置:   article > 正文

POJ 3258 River Hopscotch (二分经典)_题目大意:给出河的宽度l和n块石头,现在要求移除m块石头,

题目大意:给出河的宽度l和n块石头,现在要求移除m块石头,
River Hopscotch
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10591 Accepted: 4543

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 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:  LN, 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).

Source

题意:

一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L。河中有n块石头,每块石头到S都有唯一的距离问现在要移除m块石头(S和E除外),每次移除的是与当前最短距离相关联的石头,要求移除m块石头后,使得那时的最短距离尽可能大,输出那个最短距离。

思路:用二分枚举最小距离。找到最优解

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7. int i,j,l,m,n,dist[51000];
  8. while(scanf("%d%d%d",&l,&n,&m)!=EOF)
  9. {
  10. memset(dist,0,sizeof(dist));
  11. dist[0]=0;
  12. dist[n+1]=l;
  13. int low=l;
  14. int high=l;
  15. for(i=1;i<=n+1;i++)
  16. {
  17. if(i<=n)
  18. scanf("%d",&dist[i]);
  19. if(low>dist[i]-dist[i-1])
  20. low=dist[i]-dist[i-1];
  21. }
  22. sort(dist,dist+n+2);
  23. while(low<=high)
  24. {
  25. int mid=(low+high)/2;
  26. int num=0;
  27. int sum=0;
  28. for(i=1;i<=n+1;)
  29. {
  30. if((sum+=dist[i]-dist[i-1])<=mid)
  31. {
  32. i++;
  33. num++;
  34. }
  35. else
  36. {
  37. i++;
  38. sum=0;
  39. }
  40. }
  41. if(num<=m)//注意,当num==m时也不一定是最优解
  42. {
  43. low=mid+1;
  44. }
  45. else
  46. {
  47. high=mid-1;
  48. }
  49. }
  50. printf("%d\n",low);
  51. }
  52. return 0;
  53. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/722772
推荐阅读
相关标签
  

闽ICP备14008679号