人生列车

follow on!success!

导航

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

试题题目:

 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);
}
}

参考网址:http://blog.csdn.net/abcjennifer/article/details/5922699

解题感悟:

本题的题意已经完全理解清楚了,但是在书写代码时遇见了很多的问题。过程很清楚,甚至可以通过一个递归的思想来解答它。但是在变量处理的过程中还是出现了很混乱的感觉,这个问题让我感觉很是困惑,不知道自己问题出现在哪里……

自己的部分代码:

(未AC)

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

int main()
{
int N,L,M;
cin>>L>>N>>M;
int *num =new int [N + 2];
num[0] = 0;
for(int i=1;i<=N;i++){
cin>>num[i];
}
sort(num+1,num+N);
num[N+1] = L;
//一定会有一个简单的规律的,好好找找看
//注意题目给的条件是开始的石头和结束的石头都是不能移动的
//先申请一定的空间来保存间隔
int *dis = new int [N+2];
// dis[0]=0;
for(int j = 1;j <= N;j++){
dis[j] = num[j] - num[j-1];
}

//下面的操作就是一直不停的寻找最小值,然后将其改变即可
for(int t = 0;t < M;t++){//要求至少取M个石头,然后将它们移动好即可
int aim = 1;
for(int e = 2;e <= N+1; e++)
{
if(num[e]<num[aim])aim=e;
}
if(aim!=1||aim!=N-t){
if(num[aim+1]>num[aim-1]){num[aim-1]+=num[aim];for(int k = aim;k< N+1 -t;k++){num[k]=num[k+1];}}
else {num[aim]+=num[aim+1];for(int k = aim+1;k< N+1 -t;k++){num[k]=num[k+1];}
}
}

else
if(aim==1){num[1]=num[1]+num[2];for(int r = 2;r< N+1-t;r++)
{
num[r]=num[r+1];
}}
else
num[N-t-1] += num[N-t];
}

//寻找最小值
int zhangjie = 1;
for(int w = 2;w<=N+1-M;w++){

if(num[zhangjie]>num[w])zhangjie=w;
}
cout<<num[zhangjie]<<endl;
return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

感觉本题的解题套路都可以按照一定的方式记忆下来了,值得以后的参考

posted on 2014-07-19 12:47  tianxia2s  阅读(157)  评论(0编辑  收藏  举报