noip2015Day2T1-跳石头

题目描述 Description

一年一度的“跳石头”比赛又要开始了! 

这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石。组委会已经选择好了两块岩石作为比赛起点和终点。在起点和终点之间,有N块岩石(不含起点和终点的岩石)。在比赛过程中,选手们将从起点出发,每一步跳向相邻的岩石,直至到达终点。 

为了提高比赛难度,组委会计划移走一些岩石,使得选手们在比赛过程中的最短跳跃距离尽可能长。由于预算限制,组委会至多从起点和终点之间移走M块岩石(不能移走起点和终点的岩石)。

输入描述 Input Description

输入文件名为 stone.in。 

输入文件第一行包含三个整数L,N,M,分别表示起点到终点的距离,起点和终点之间的岩石数,以及组委会至多移走的岩石数。 

接下来N行,每行一个整数,第i行的整数Di(0 < Di < L)表示第i块岩石与起点的距离。这些岩石按与起点距离从小到大的顺序给出,且不会有两个岩石出现在同一个位置。 

输出描述 Output Description

输出文件名为stone.out。 

输出文件只包含一个整数,即最短跳跃距离的最大值。

样例输入 Sample Input

25 5 2

11 

14 

17

21

样例输出 Sample Output

4

数据范围及提示 Data Size & Hint

对于20%的数据,0≤M≤N≤10。 对于50%的数据,0≤M≤N≤100。 

对于50%的数据,0≤M≤N≤100。

对于100%的数据,0≤M≤N≤50,000,1≤L≤1,000,000,000。

——————————————————————题解

这道题要二分答案,非常……简单

可我当时不会二分,end.

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdlib>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <algorithm>
 7 using namespace std;
 8 int  a[50005],l,m,n;
 9 
10 int check(int x)
11 {
12     int j=1 ,i=0,ans=0;
13     while(i<=n)
14     {
15         j=i+1;
16         while(a[j]-a[i]<x&&j<=n+1)
17             j++;
18         ans+=j-i-1;//因为算的是间距,所以还要-1 
19         i=j;
20     }
21     if(ans>m)
22         return 1;
23     else
24         return 0;
25     
26 }
27 int main()
28 {
29     freopen("f1.in","r",stdin);
30     int left , right;
31     scanf("%d%d%d",&l,&n,&m);
32     a[0]=0;
33     a[n+1]=l;
34     for(int i=1;i<=n;i++)
35     {
36         scanf("%d",&a[i]);
37     }
38     right=l/(n-m+1);
39     left=0;
40     int temp;
41     while (left<right)
42     {
43         temp=(left+right+1)/2;
44         if(check(temp))
45             right=temp-1;
46         else
47             left=temp;
48     }
49     printf("%d
",left);
50     return 0;
51 }
原文地址:https://www.cnblogs.com/ivorysi/p/5804750.html