HDUoj 4004 The Frog's Games ( 二分
来源:互联网 发布:知乎 bipolar disorder 编辑:程序博客网 时间:2024/05/23 14:56
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
题意
青蛙举行了运动会,要求青蛙跳跃小河。河流宽度为 L (1<= L <= 1000000000). 河里会有 n 个石头沿着垂直于河岸的直线排成一排,青蛙以跳到石头上,然后再次跳跃。青蛙最多能够跳 m 次;现在青蛙们想知道他们最少应该有多大的跳跃能力才能够到达河对岸?
题解:
二分的经典题 最大值最小化 贪心判定+二分查询 寒假前写着毫无思路 QAQ 现在看水题一枚
AC代码
#include <bits/stdc++.h>using namespace std;const int N = 1e6;int arr[N];int l, m, n;bool check(int len){ int last = 0, k = 0, i = 1; while(true) { if(arr[i] <= last+len) i++; else { if(last == arr[i-1]) return false; last = arr[i-1]; k++; } if(i>n+1) break; } k++; if(k <= m) return true;return false;}int main(){ while(~scanf("%d%d%d",&l,&n,&m)) { for(int i = 1;i <= n; i++) { scanf("%d",&arr[i]); } int ans; arr[n+1] = l; sort(arr+1,arr+n+1); int left = 1; int right = l; while(left <= right) { int mid = (left+right) >> 1; if(check(mid)) { ans = mid; right = mid-1; } else left = mid+1; } printf("%d\n",ans); }return 0;}
- HDUoj 4004 The Frog's Games ( 二分
- 【HDUOJ】4004---The Frog's Games(二分)
- HDU 4004 THE FROG’S GAMES(二分)
- HDOJ 4004 The Frog's Games 二分
- HDU 4004 The Frog's Games(二分)
- HDU 4004 The Frog's Games(二分)
- hdu 4004 The Frog's Games【二分】
- hdu 4004 The Frog's Games 【二分】
- The Frog's Games 4004 (二分)***
- HDU 4004 The Frog's Games (二分)
- 4004 The Frog's Games --二分+贪心
- HDU 4004 The Frog's Games [二分]
- HDU-4004-The Frog's Games【二分】
- 【HDU 4004 The Frog's Games】+ 二分
- HDU:4004 The Frog's Games<二分>
- Hdu 4004 The Frog's Games(二分)
- The Frog's Games 二分
- The Frog's Games(二分)
- java复习——类和对象
- 第2章 迭代、进化和敏捷
- 一、JMX官方指导文档 之 JMX概述
- LeetCode70. Climbing Stairs
- 学习淘淘商城第四十九课(搜索功能测试)
- HDUoj 4004 The Frog's Games ( 二分
- Java学习笔记(十一)--Set集合 Map集合
- Day04-三元运算符
- 流量控制和拥塞控制
- Python 字典中一键对应多个值
- matlab 音频处理
- 第十周
- cocos creator 接微信登录调试遇到的问题
- 使用servlet实现文件上传(用common-fileUpload和common-io包)