提交地址:http://codevs.cn/problem/4768/

题目:

跳石头|河中跳房子|NOIP2015提高组T4|二分法

跳石头|河中跳房子|NOIP2015提高组T4|二分法

 

 题意:自己看

思路:

1.读入各个石头数据

2.直接二分答案:

枚举一个石头i和一个石头j,要求i和j之间的距离为mid,然后删去i到j之间的石头,再将i到j之间的石头数加到ans里

然后把ans和m比较一下

然后就那么搞……

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 int main()
 8 {
 9     int L,a[50050],ans;
10     int n,m,i,j,l,r,mid,k;
11     scanf("%d %d %d",&L,&n,&m);
12     for (i=1; i<=n; i++) scanf("%d",&a[i]);
13     l=0;r=L+1;
14     a[0]=0;a[n+1]=L;
15     while (l+1<r)
16     {
17         ans=0;
18         mid=(l+r)/2;
19         i=0;
20         while (i<=n)
21         {
22             j=i+1;
23             while (j<=n+1 && a[j]-a[i]<mid) j++;
24             ans+=j-i-1;
25             i=j;
26         }
27         if (ans<=m) l=mid;
28         else r=mid;
29     }
30     printf("%d",l);
31     return 0;
32 }
跳石头

相关文章:

  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-06-30
  • 2021-09-05
  • 2021-09-23
猜你喜欢
  • 2021-09-17
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2022-01-28
  • 2021-09-29
  • 2021-08-04
相关资源
相似解决方案