首页 技术 正文
技术 2022年11月8日
0 收藏 867 点赞 1,818 浏览 1773 个字

The Frog’s Games

Problem Description

The annual Games in frogs’ kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they
are out. The frogs was asked to jump at most
m (1<= m <= n+1) times. Now the frogs want to know if they want to jump
across the river, at least what ability should they have. (That is the frog’s
longest jump distance).

Input

The input contains several cases. The first line of each case
contains three positive integer L, n, and m.
Then n lines follow. Each
stands for the distance from the starting banks to the nth stone, two stone
appear in one place is impossible.

Output

For each case, output a integer standing for the frog’s
ability at least they should have.

Sample Input

6 1 2
2
25 3 3
11
2
18

Sample Output

4
11

分析:

就是一个青蛙要跳的对岸,然后河流中间有M块石头,然后最多可以跳k次,问这个青蛙最短需要跳多远才可以保证不掉下去。按照我的想法,m个石头,都被跳到的话需要跳M+1次。那么如果k >= M+1次,那就是找到两块石头之间距离最大的哪一个就可以了。但是如果k < M+1次的话呢,青蛙就需要一次跳过多的石头,M+1 – k.就可以知道青蛙需要越过多少块石头,既在多少块石头上面不能停留。所以呢,我就需要每次去遍历,找到最近的相隔一块石头的两块石头。那么我就需要循环遍历(M+1-k) * (M+1)  次左右了。这肯定不行啊,大致估计一下时间复杂度500000 * 500000,GG,肯定会超时。想了一想,想不到好办法,然后百度,说用二分,去二分所要求的跳跃能力,然后每次检验一下能不能跳过去。这样再算一下时间,500000 * log(1000000000 ),这不会超时。应该记住的 n*2 的时间优化多半是优化到n*log(n), 而log(n)的优化,多半是二分,或者一些STL的数据结构。

 #include<bits/stdc++.h> using namespace std; const int N = ; int ans[N];  int ll, nn, mm; bool judge (int dis) {
int tx = , i, ty = ;
for (int i = ; i <= mm; i++) {
while (ty <= nn + && ans[ty] - ans[tx] <= dis) ty++; ty --;
tx = ty;
}
return ty == nn + ;
} int main () { while (~scanf("%d %d %d", &ll, &nn, &mm)) { for (int i = ; i <= nn; i++) {
scanf("%d", &ans[i]);
}
ans[] = ; ans[nn+] = ll;
sort(ans, ans + nn +);
int L = , R = ll;
while (L <= R) {
int mid = (L + R) >> ;
if (judge(mid)) {
R = mid - ;
} else L = mid + ;
}
printf("%d\n", L);
}
return ;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295