首页 > 代码库 > 2014年7月19日——比赛题取石头问题1
2014年7月19日——比赛题取石头问题1
参考地址:
http://blog.csdn.net/abcjennifer/article/details/5922699
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 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 2214112117
Sample Output
4
Hint
Source
#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;
}
其实自己的思路一开始并不是使用二分法,而是想通过寻找到最小的那个路径然后将它去掉,然后重新搜索,搜索的次数即为去掉石头的个数,所以思路有点偏。