poj 3258 River Hopscotch

来源:互联网 发布:倚天行情软件下载 编辑:程序博客网 时间:2024/06/05 15:28

题目链接:点这里。

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

Line 1: Three space-separated integers: L, N, and MLines 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

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).

【题意】

一条河,河中有一些石子,假设只能从石子上走,显然根据这些石子可以得到一个最短的相邻石子的距离,现在问你搬走一些石子,使得剩下的石子中相邻石子的距离的最小值为多少。其中要求你自己选择石子搬走,并使这个最小值最大。、

【分析】

首先就是石子的数量很少,但是两个石子之间的距离可能很大,可能很小。显然是
不能枚举搬走几个石子,或者说不能枚举每个石子是否搬走。所以显然是二分答案。然后根据贪心的思想,每次两个石子的距离小于mid的时候说明这里应该搬走石子了。然后统计当答案是mid的时候是否符合题意。然后直接二分就行了。

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<set>#include<map>#include<queue>#include<sstream>using namespace std;#define rep0(i,l,r) for(int i = (l);i < (r);i++)#define rep1(i,l,r) for(int i = (l);i <= (r);i++)#define rep_0(i,r,l) for(int i = (r);i > (l);i--)#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)#define MS0(a) memset(a,0,sizeof(a))#define MS_1(a) memset(a,-1,sizeof(a))#define MSinf(a) memset(a,0x3f,sizeof(a))#define MSfalse(a) memset(a,false,sizeof(a))#define pin1(a) printf("%d",(a))#define pin2(a,b) printf("%d%d",(a),(b))#define pin3(a,b,c) printf("%d%d%d",(a),(b),(c))#define pll1(a) printf("%lld",(a))#define pll2(a,b) printf("%lld%lld",(a),(b))#define pll3(a,b,c) printf("%lld%lld%lld",(a),(b),(c))#define pdo1(a) printf("%f",(a))#define pdo2(a,b) printf("%f%f",(a),(b))#define pdo3(a,b,c) printf("%f%f%f",(a),(b),(c))#define huiche puts("")#define inf 0x3f3f3f3f#define lson i<<1,l,mid#define rson ((i<<1)|1),mid+1,r#define uint unsigned inttypedef pair<int,int> PII;#define A first#define B second#define pb push_back#define MK make_pair#define ll long long#define eps 1e-8inline void read1(int &num) {    char in;    bool IsN=false;    in=getchar();    while(in!='-'&&(in<'0'||in>'9')) in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else num=in-'0';    while(in=getchar(),in>='0'&&in<='9') {        num*=10,num+=in-'0';    }    if(IsN) num=-num;}inline void read2(int &a,int &b) {    read1(a);    read1(b);}inline void read3(int &a,int &b,int &c){    read1(a);    read1(b);    read1(c);}inline void read1(ll &num) {    char in;    bool IsN=false;    in=getchar();    while(in!='-'&&(in<'0'||in>'9')) in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else num=in-'0';    while(in=getchar(),in>='0'&&in<='9') {        num*=10,num+=in-'0';    }    if(IsN) num=-num;}inline void read2(ll &a,ll &b) {    read1(a);    read1(b);}inline void read3(ll &a,ll &b,ll &c){    read1(a);    read1(b);    read1(c);}inline void read1(double &num) {    char in;    double Dec=0.1;    bool IsN=false,IsD=false;    in=getchar();    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))        in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else if(in=='.') {        IsD=true;        num=0;    } else num=in-'0';    if(!IsD) {        while(in=getchar(),in>='0'&&in<='9') {            num*=10;            num+=in-'0';        }    }    if(in!='.') {        if(IsN) num=-num;        return ;    } else {        while(in=getchar(),in>='0'&&in<='9') {            num+=Dec*(in-'0');            Dec*=0.1;        }    }    if(IsN) num=-num;}inline void read2(double &a,double &b){    read1(a);    read1(b);}inline void read3(double &a,double &b,double &c){    read1(a);    read1(b);    read1(c);}///----------------------------------------------------------------------------------------------------------------------------int num[50000+100];int main() {//    freopen("in.txt","r",stdin);    int l,r,n,x;    while(scanf("%d",&r)!=EOF)    {        l = inf;        read2(n,x);        rep1(i,1,n) read1(num[i]);        num[n+1] = r;        sort(num+1,num+n+2);        rep1(i,0,n) {num[i]=num[i+1]-num[i];l=min(l,num[i]);}        int ans=r;        while(l<=r)        {            int mid = (l+r)>>1;            int xx = 0;            int t = 0;            rep1(i,0,n)            {                if((t+=num[i])>=mid) t=0;                else xx++;                if(xx>x) break;            }            if(xx<=x) l = mid+1,ans=mid;            else r = mid-1;        }        printf("%d\n",ans);    }    return 0;}
原创粉丝点击