bzoj1650 / P2855 [USACO06DEC]河跳房子River Hopscotch / P2678 (noip2015)跳石头
P2855 [USACO06DEC]河跳房子River Hopscotch
二分+贪心
每次二分最小长度,蓝后检查需要去掉的石子数是否超过限制。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define N 50010 7 int n,m,L,a[N]; 8 bool check(int lim){ 9 int k=0; 10 for(int i=1,j=0;i<=n;++i){ 11 if(a[i]-a[j]<lim) ++k; 12 else j=i; 13 }return k<=m; 14 } 15 int main(){ 16 scanf("%d%d%d",&L,&n,&m); a[++n]=L; 17 for(int i=1;i<n;++i) scanf("%d",&a[i]); 18 sort(a+1,a+n+1); 19 int l=0,r=L; 20 while(l<r){ 21 int mid=l+((r-l)>>1); 22 if(check(mid)) l=mid+1; 23 else r=mid; 24 }printf("%d",check(l)?l:l-1); 25 return 0; 26 }