码迷,mamicode.com
首页 > 其他好文 > 详细

4768 跳石头

时间:2017-04-24 23:07:17      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:位置   view   span   ref   desc   提高   二分答案   文件   put   

4768 跳石头

 

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
 
 
 
 
题目描述 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。

 

分类标签 Tags

经典的二分答案问题

这里我巧妙的运用了L=mid+1来输出结果

其他的基本就是套模板

注意last指针的用法

指向上一个处理过的石头

 

 

一点懵逼却得到70分的题解(应该是last没有处理好)

 

技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN=100001;
 7 int cd[MAXN];
 8 int bc[MAXN];
 9 int l,n,m;
10 int minn=1;
11 int maxn=-1;
12 int tot=0;
13 int ls[MAXN];
14 int pd(int p)
15 {
16     tot=0;
17     for(int i=1;i<=n;i++)
18     ls[i]=cd[i];
19     for(int i=1;i<=n;i++)
20     {
21         if(ls[i]<p)
22         {
23             tot++;
24             ls[i+1]=ls[i+1]+ls[i];
25         }
26     }
27     if(tot==m)
28     return 1;
29     else
30     return 0;
31 }
32 void rf()
33 {
34     int ans=l;
35     int L=1,R=maxn;
36     while(L<=R)
37     {
38         int mid=(L+R)/2;
39         if(pd(mid)==1)
40         {
41             ans=mid;
42             L=mid+1;
43         }
44         else
45         {
46             if(tot>m)
47             R=mid-1;
48             else 
49             L=mid+1;
50         }
51     }
52     printf("%d",ans);
53 }
54 int main()
55 {
56     
57     scanf("%d%d%d",&l,&n,&m);
58     for(int i=1;i<=n;i++)
59     {
60         scanf("%d",&bc[i]);
61     }
62     maxn=l;
63     for(int i=1;i<=n;i++)
64         cd[i]=bc[i]-bc[i-1];
65     rf();
66     return 0;
67 }
View Code

 

 

 

AC

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN=100001;
 7 int bc[MAXN];
 8 int l,n,m;
 9 int tot=0;
10 int ls[MAXN];
11 
12 int pd(int p)
13 {
14     int last=0;
15     int tot=0;
16     for(int i=1;i<=n;i++)
17     {
18         if(bc[i]-last<p)
19         tot++;
20         else 
21         last=bc[i];
22     }
23     if(tot>m)
24     return 0;
25     else 
26     return 1;
27     /*else 
28     return 0;*/
29 }
30     
31 int main()
32 {
33     scanf("%d%d%d",&l,&n,&m);
34     for(int i=1;i<=n;i++)
35         scanf("%d",&bc[i]);
36     bc[n+1]=l;
37     n++;
38     int L=1,R=l;
39     while(L<=R)
40     {
41         int mid=(L+R)/2;
42         if(pd(mid)==1)
43         {
44             //ans=mid;
45             L=mid+1;
46         }
47         else
48             R=mid-1;
49     }
50     printf("%d",L-1);
51     return 0;
52 }

 

4768 跳石头

标签:位置   view   span   ref   desc   提高   二分答案   文件   put   

原文地址:http://www.cnblogs.com/zwfymqz/p/6759151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!