标签:placed 题意 ESS mes instead this mission more font
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21939 | Accepted: 9081 |
Description
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
Output
Sample Input
25 5 2 2 14 11 21 17
Sample Output
4
Hint
【题意】
在起点和终点之间,有 N 块岩石(不含起点和终点的岩石),至多可以从起点和终点之间移走 M 块岩石(不能移走起点和终点的岩石),求移走岩石后最短跳跃距离的最大值
【分析】
[NOIP2015跳石头(原题)]
直接二分,具体详见代码
【代码】
#include<cstdio> #include<algorithm> #include<iostream> #define debug(x) cerr<<#x<<" "<<x<<‘\n‘; using namespace std; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } const int N=1e5+5; int ans,n,m,L,d[N]; inline bool check(int now){ int res=0,p=0;//p为前一个石头到指向石头的距离 for(int i=1;i<=n;i++){ if(d[i]-p<now){//如果第i个石头到前一个石头的距离小于假设的答案 res++;//该石头可以搬走 } else{//如果大于(即满足条件) p=d[i];//则保留该石头,并将p记录 下一个石头到该石头的距离 } } return res<=m; } int main(){ L=read(),n=read(),m=read(); for(int i=1;i<=n;i++) d[i]=read();d[++n]=L;//我们假设在终点处有第n+1个石头,则他到起点的距离为L sort(d+1,d+n+1);//保证石头有序 int l=0,r=L,mid; while(l<=r){ mid=l+r>>1; if(check(mid)){//说明二分结果小了,要向右二分 ans=mid;//记录答案 l=mid+1; } else{ r=mid-1;//说明结果大了,要向左二分 } } printf("%d\n",ans); return 0; }
POJ 3258 River Hopscotch(二分答案)
标签:placed 题意 ESS mes instead this mission more font
原文地址:https://www.cnblogs.com/shenben/p/10311923.html