NOIP 跳石头

不说了还是二分,挑战自己码代码的速度

每次二分最远的距离就可以了

最后不知道怎么要判断下r

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int a[100005],n,m;
 5 int check(int x){
 6     int nowstone=1;
 7     int dist=a[1];
 8     int stone=0;
 9     while(nowstone<=n){
10         while(dist<x){
11             nowstone++;
12             stone++;
13             if(stone>m)return 0;
14             dist+=a[nowstone]-a[nowstone-1];
15         }
16         dist=a[nowstone+1]-a[nowstone];
17         nowstone++;
18     }
19     return 1;
20 }
21 int main(){
22     int leng;
23     cin>>leng>>n>>m;
24     for(int i=1;i<=n;i++){
25         cin>>a[i];
26     }
27     a[n+1]=leng;
28     int l=0,r=leng;
29     int ans;
30     while(l<r){
31         int mid=l+r>>1;
32         if(check(mid))ans=mid,l=mid+1;
33         else r=mid-1;
34     }
35     if(check(r))ans=r;
36     cout<<ans;
37 }

代码能力勉强还能达到要求了

猜你喜欢

转载自www.cnblogs.com/saionjisekai/p/9643511.html