码迷,mamicode.com
首页 > 其他好文 > 详细

2014年7月19日——比赛题取石头问题1

时间:2014-07-19 18:07:48      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   使用   strong   os   

参考地址:

http://blog.csdn.net/abcjennifer/article/details/5922699

River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2433   Accepted: 1064

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

二分判断每个长度的间隔是否能满足去掉的石头数<=m
试题
参考AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
#define N 50010
int len[N];

int n,m,l;
bool judge(int inv)//interval
{
int seq[N],top=0,num=0;
seq[0]=-1;
for(int i=0;i<n;i++)
{
if(len[i]-seq[top]<inv)
num++;
else
seq[++top]=len[i];
}
if(l-seq[top]<inv)
return false;
if(num>m)
return false;
return true;
}

int main()
{
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
int i,j,k;
for(i=0;i<n;i++)
scanf("%d",&len[i]);
sort(len,len+n);
int ans;
int low=0,high=l;
while(low<=high)
{
int mid=(low+high)>>1;
if(judge(mid))
{
low=mid+1;
ans=mid;
}
else
high=mid-1;
}
printf("%d/n",ans);
}
}

自己的看法和总结:

这道题的思想是经常遇见,值得自己以后参考借鉴。

代码思路:

思路:二分答案,判断该答案是否正确。
比如搜索到答案D,可以从头搜索每个点,如果该点与前一个点的距离小于D,则取掉这个点,最后统计去掉了都少个点。如果比m多了,说明D大了,向下二分。否则向上二分。

 

 

因此后来就将这个代码看懂了。

#include <iostream>
#include <algorithm>
using namespace std;

int a[50001], n, m, l, mid;

bool check(int mid){
int stack[50001], point = 0, count = 0;
stack[0] = 0;
for (int i = 0; i < n; i++){
if (a[i] - stack[point] < mid) count++;
else stack[++point] = a[i];
}
if (l - stack[point] < mid || count > m) return false;
return true;
}

int main(){
while(cin>>l>>n>>m){
for (int i = 0; i < n; i++) cin>>a[i];
sort(a, a + n);
int l1 = 0, l2 = l;
while (l1 < l2){
mid = (l1 + l2) >> 1;
if (check(mid)) l1 = mid; else l2 = mid - 1;
if (l1 + 1 == l2) l1 = l2;
}
mid = (l1 + l2) >> 1;
if (!check(mid)) l1--;
cout<<l1<<endl;
}
return 0;
}

其实自己的思路一开始并不是使用二分法,而是想通过寻找到最小的那个路径然后将它去掉,然后重新搜索,搜索的次数即为去掉石头的个数,所以思路有点偏。

2014年7月19日——比赛题取石头问题1,布布扣,bubuko.com

2014年7月19日——比赛题取石头问题1

标签:des   blog   http   使用   strong   os   

原文地址:http://www.cnblogs.com/tianxia2s/p/3854923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!