The Frog's Games
The Frog's Games
Problem Description
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
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
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 ;
}
The Frog's Games的更多相关文章
- The Frog's Games(二分)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- HDU 4004 The Frog's Games(二分答案)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- HDUOJ----4004The Frog's Games(二分+简单贪心)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- hdu 4004 The Frog's Games
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 The annual Games in frogs' kingdom started again ...
- H - The Frog's Games
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...
- 杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)
Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog T ...
- HDU-4004 The Frog's Games (分治)
http://acm.hdu.edu.cn/showproblem.php?pid=4004 Problem Description The annual Games in frogs' kingdo ...
- D - The Frog's Games (二分)
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...
随机推荐
- vue 路由拦截器和请求拦截器
路由拦截器 已路由为导向 router.beforeEach((to,from,next)=>{ if(to.path=='/login' || localStorage.getItem('to ...
- docker容器共享宿主机环境,从而为镜像体积减负
一.背景介绍 响应公司技术发展路线,开发的服务均需要将打成docker镜像,使用docker进行统一管理.可是随着服务越来越多,镜像也越来越多.每次制作镜像的时候都需要将依赖打进容器,这样一个jre的 ...
- jsonp跨域实例
一.什么是跨域 二.如何解决跨域 1.前端常用 JSONP 2.服务器端配置 HTTP 协议的 header 解析 三.JSONP实现的实例 <!DOCTYPE html> <htm ...
- HTTP与HTTPS区别(详细)
转:http://blog.sina.com.cn/s/blog_6eb3177a0102x66r.html 1.减少http请求(合并文件.合并图片)2.优化图片文件,减小其尺寸,特别是缩略图,一定 ...
- C# for循环测试
- Qt第三方库----QCustomPlot
一.软件下载 下载地址:http://www.qcustomplot.com/index.php/download 这里推荐下载第一个链接的内容: 注:这里的第三方库要放在非中文目录下. 二.配置 ( ...
- TTTTTTTTTTTTTTTTTT POJ 1330
题意:给一个有根树,一个查询节点(u,v)的最近公共祖先: #include <iostream> #include <cstdio> #include <cstring ...
- SPFA算法的判负环问题(BFS与DFS实现)
经过笔者的多次实践(失败),在此温馨提示:用SPFA判负环时一定要特别小心! 首先SPFA有BFS和DFS两种实现方式,两者的判负环方式也是不同的. BFS是用一个num数组,num[x] ...
- ES6 函数参数的解构赋值
function add([x, y]){ return x + y; } add([1, 2]); // 3
- 微信小程序 API 网络(ajax)
网络 API 类似于 ajax 向服务器请求网络地址,唯一不同的是这个请求有很多的规则,且必须向服务器上请求,不能在本地请求 网络 发送请求: wx.request() 发起https网络请求 参数: ...