今天广州站亚洲区域赛热身,他们都去当志愿者了,我没衣服
题意:
跳过宽度为l的河,河中有n个石头,直线排列,坐标已知,设跳跃一次最远为x,要求跳m次以下,问x最小是多少
Input
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
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.
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
For each case, output a integer standing for the frog's ability at least they should have.
水题二分搜索即可,每次尝试能否跳过去,跳过去需要多少次跳跃
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define mxn 501000
int a[mxn];
int L, n, m;
int judge( int x ){
int cnt = 0;
int pos = 0;
for( int i = 1; i <= n + 1; ++i ){
/* if(a[i] - a[pos] > x){
if(i-1==pos) return 0;
pos=i-1;
++cnt;
--i;
}*/
if( a[i] - a[i-1] > x )
return 0;
if( a[i] - a[pos] > x ){
cnt++;
pos = i - 1;
}
if( i == n )
cnt++;
}
return cnt <= m;
}
int main()
{
while( scanf("%d%d%d",&L,&n,&m) != EOF ){
a[0] = 0;
for( int i = 1; i <= n; ++i )
scanf("%d",&a[i]);
a[n+1] = L;
sort( a, a + n + 1 );
int LL = 0;
int RR = L + 1;
int ans = L + 1;
while( LL + 1 < RR ){
int m = ( LL + RR ) >> 1;
// cout<<"ll "<<LL<<" m "<<m<<" RR "<<RR<<endl;
if( judge( m ) ){
RR = m;
ans = m;
}
else LL = m;
}
printf("%d\n",ans);
}
return 0;
}