poj 3258 River Hopscotch

来源:互联网 发布:带端口的域名解析 编辑:程序博客网 时间:2024/06/14 03:52

题目链接:点击打开链接

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 2214112117

Sample Output

4

ps:主要是读懂题的啊,,,
题目大意:有一个农场,里面有牛,农主要锻炼牛的跳跃能力,于是就让牛在河里的石头上跳跃,河里有N个石头,不包括起点和终点,给出河的长度,然后问你要从河中拿掉m个石头后一个牛要跳到终点的最小跳跃的最大值(好罗嗦,晕了).

基本思路:这和之前坐过的一个题类似的,就是求最小值的最大化,用排序+二分.这里的最小跳跃是一次所跳的距离,当然是从距离最小的开始枚举了,而最大值是要根据拿掉m个石头后所满足的距离,二分枚举.

#include <iostream>#include<algorithm>#include<cstdlib>#include<cmath>using namespace std;int  dist[50100];int main(){    int L,n,m;    while(cin>>L>>n>>m)    {        dist[0]=0;        dist[n+1]=L;        for(int i=1; i<=n; i++)        {            cin>>dist[i];        }        sort(dist,dist+1+n);///最小距离的话要先排序(从小到大)        int left=0x3f3f3f3f;        int right=L;        for(int i=1; i<=n+1; i++)        {            left=min(left,dist[i]-dist[i-1]);        }        int ans=0,sum=0;        while(right>=left)        {            ans=sum=0;            int mid=(left+right)/2;            for(int i=1; i<=n+1; i++)            {                sum=sum+dist[i]-dist[i-1];                if(sum<=mid)///因为是求最大跳跃的最小值,所以从前往后                    ///把石头拿走,判断是否符合要求                {                    ans++;                }                else                {                    sum=0;                }            }            if(ans<=m)left=mid+1;            else right=mid-1;        }        cout<<left<<endl;    }    return 0;}


0 0
原创粉丝点击