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 ;
}

The Frog's Games的更多相关文章

  1. The Frog's Games(二分)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  2. HDU 4004 The Frog's Games(二分答案)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  3. HDUOJ----4004The Frog's Games(二分+简单贪心)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  4. 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) ...

  5. hdu 4004 The Frog's Games

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 The annual Games in frogs' kingdom started again ...

  6. H - The Frog's Games

    The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...

  7. 杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)

    Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog T ...

  8. HDU-4004 The Frog's Games (分治)

    http://acm.hdu.edu.cn/showproblem.php?pid=4004 Problem Description The annual Games in frogs' kingdo ...

  9. D - The Frog's Games (二分)

    The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...

随机推荐

  1. chrome scrollTop 获取失败问题及解决方案

    https://blog.csdn.net/h357650113/article/details/78384621

  2. vue2.0关于添加属性后视图不能更新的问题

    属性赋值和this.$set 和vue.$set方法我不行 可以用 this.$delete来进行删除后在设置都可以了

  3. Codeforces 988D Points and Powers of Two ( 思维 || 二的幂特点 )

    题目链接 题意 : 给出坐标轴上的 n 个点的横坐标,要你选出最多的点,使得这些点两两距离是二的幂 ( 特殊情况 : 选出的集合只有一个点也满足条件 ) 分析 : 官方题解已经说的很好了 最关键是是判 ...

  4. select客户端模型封装——回调方式快速建立客户端

    SockClient.h #pragma once #include<functional> #define _WINDOWS #ifdef _WINDOWS #define _WINSO ...

  5. sh_02_第一个while循环

    sh_02_第一个while循环 # 打印 5 遍 Hello Python # 1. 定义一个整数变量,记录循环次数 i = 1 # 2. 开始循环 while i <= 3: # 1> ...

  6. javascript二叉树

    javascript中的二叉树一(binary tree) 毕业也快2年了,毕业之后就没有认真写过博客了,都是那里学习一下,这里弄一下.学习了也不做笔记,过后就忘记了.我对这种状态打从心里是讨厌的. ...

  7. 案例ORA-00600: internal error code, arguments: [qkaffsindex3], [], [], [], []

    执行更新统计信息语句: exec dbms_stats.gather_schema_stats(ownname=>'LIVE_KS',degree=>2,cascade=>true, ...

  8. Eigen中的矩阵及向量运算

    Eigen中的矩阵及向量运算 ,[+,+=,-,-=] ,[\*,\*=] ,[.transpose()] ,[.dot(),.cross(),.adjoint()] ,针对矩阵元素进行的操作[.su ...

  9. loj#6034 「雅礼集训 2017 Day2」线段游戏

    分析 区间李超树板子题 代码 #include<bits/stdc++.h> using namespace std; #define db double const int inf = ...

  10. Windows环境下Mysql 5.7读写分离简单记录

    一.目的 本文记录了在Windows环境中,mysql数据库读写分离配置过程. 二.准备: Master机器:Windows 10 虚拟机,IP:192.168.3.32 Slave机器:Window ...