hdoj 4004The Frog's Games解题报告

hdoj 4004-The Frog's Games链接http://acm.hdu.edu.cn/showproblem.php?pid=4004

这道题是11年大连赛区网络赛的一道题,当时刚刚学ACM没做出来,现在在看看,百感交集啊,只能说自己当时太水了。解释在代码的注释中

View Code
 1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 int stone[500005];
5 int s,l;
6 int cmp(const void *a,const void *b)
7 {
8 return *(int *)a-*(int *)b;
9 }
10 bool check(int mid,int m)//判断此时的mid即跳跃最大距离能否满足在m步内完成
11 {
12 int i;
13 int dis=0;
14 if(mid<stone[0])
15 return false;
16 for(i=1;i<=s;i++)
17 {
18 if(mid<stone[i]-dis&&mid>=stone[i-1]-dis)
19 {
20 m--;
21 dis=stone[i-1];
22 }
23 if(mid<stone[i]-dis)
24 return false;
25 if(m<=0)
26 return false;
27 }
28 if(stone[s]-dis<=mid)
29 return true;
30 return false;
31 }
32
33 int main()
34 {
35 int mid;
36 int i,m;
37 while(scanf("%d%d%d",&l,&s,&m)!=EOF)
38 {
39 for(i=0;i<s;i++)
40 scanf("%d",&stone[i]);
41 qsort(stone,s,sizeof(int),cmp);
42 stone[s]=l;
43 int low=0;
44 int high=l;
45 int ans;
46 while(low<=high)
47 {
48 mid=(low+high)>>1;
49 if(check(mid,m))//如果满足,接着减小mid
50 {
51 ans=mid;
52 high=mid-1;
53 }
54 else
55 low=mid+1;//否则要使mid增加
56 }
57 printf("%d\n",ans);
58 }
59 return 0;
60 }
原文地址:https://www.cnblogs.com/caozhenhai/p/2433761.html