Problem Description
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
Input
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
Output
Sample Input
6 1 2 2 25 3 3 11 2 18
Sample Output
4 11
因为数据量是10^9,直接找1s会T,(1s处理数据量为10^6~10^7,撑死10^8),所以二分求跳跃距离(时间复杂度为logN);
用dis数组存储两个石子间的距离,然后从两石子间最大距离~L二分,直到left=right;
直接二分时mid是偏左的,所以修改左端点时mid+1;
求以x距离跳跃时步数,外循环i控制跳到哪一个dis,内循环j控制跳几个dis,一旦x<dis之和就steps++;
最后记得将i设置到即将要跳的dis,并且步数+1(因为求出来的i是到第n-1个dis);
做了好久的题啊……看了题解并没有理解最后还是自己一步一步模拟过程才懂的;
独立思考很重要!队友给了我很多提示,但是自己不把这个过程想明白永远都做不出来!
学会了新姿势system("pause")。。简直是我这种喜欢分析过程人的福音。。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=500010;
int a[N];
int dis[N];
int L,n,m;
int jump(int x){
int steps=0,i=1,j=0,sum=0;
while(i<n){
sum=0;
for(j=i;j<=n;j++){
sum=sum+dis[j];
if(x<sum){
steps++;
break;
}
}
i=j;
}
return steps+1;
}
int main(){
while(~scanf("%d %d %d",&L,&n,&m)){
int maxdis=0;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+n+1);
a[0]=0;a[n+1]=L;
for(int i=1;i<=n+1;i++){
dis[i]=a[i]-a[i-1];
maxdis=max(dis[i],maxdis);
}
if(m==1){
printf("%d\n",L);
continue;
}
else if(m==n+1){
printf("%d\n",maxdis);
continue;
}
n=n+1;
int left=maxdis,right=L;
while(left<right){
int mid=(left+right)/2;
// system("pause");
// cout<<mid<<" "<<jump(mid)<<endl;
if(jump(mid)<=m){
right=mid;
}
else {
left=mid+1;
}
}
printf("%d\n",left);
}
}
9931



被折叠的 条评论
为什么被折叠?



