Problem Description
Once upon a time, there is a little frog called Matt. One day, he came to a river.



The river could be considered as an axis.Matt is standing on the left bank now (at position 0). He wants to cross the river, reach the right bank (at position M). But Matt could only jump for at most L units, for example from 0 to L.



As the God of Nature, you must save this poor frog.There are N rocks lying in the river initially. The size of the rock is negligible. So it can be indicated by a point in the axis. Matt can jump to or from a rock as well as the bank.



You don't want to make the things that easy. So you will put some new rocks into the river such that Matt could jump over the river in maximal steps.And you don't care the number of rocks you add since you are the God.



Note that Matt is so clever that he always choose the optimal way after you put down all the rocks.
 
Input
The first line contains only one integer T, which indicates the number of test cases.



For each test case, the first line contains N, M, L (0<=N<=2*10^5,1<=M<=10^9, 1<=L<=10^9).



And in the following N lines, each line contains one integer within (0, M) indicating the position of rock.
 
Output
For each test case, just output one line “Case #x: y", where x is the case number (starting from 1) and y is the maximal number of steps Matt should jump.
 
Sample Input
2
1 10 5
5
2 10 3
3
6
 
Sample Output
Case #1: 2
Case #2: 4
 
Source

题意:有一条小河长为M的小河。能够看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙。任意加入石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多须要多少次。

思路:贪心的做法,显然假设每次都让青蛙在L+1的长度跳两次是最优的。那么问题来了:假设要这么跳的话,那么在L+1的地方有一个石子。在[0, L+1]的地方也要有一个位置,可是要考虑到前面青蛙已经跳到的位置。由于我们是希望它仅仅能从0的位置開始跳的,假设前面的位置last+x(这段距离多出来的部分)<L+1的话,那么青蛙就能够跳过这个部分,我认为画张图比較好理解一点

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 200005; int n, m, l;
int num[maxn]; int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &n, &m, &l);
for (int i = 1; i <= n; i++)
scanf("%d", &num[i]);
num[0] = 0, num[++n] = m; int ans = 0;
int last = l;
sort(num, num+n);
for (int i = 1; i <= n; i++) {
int x = (num[i] - num[i-1]) % (l+1);
int y = (num[i] - num[i-1]) / (l+1);
if (last + x >= l+1) {
last = x;
ans += 2 * y + 1;
}
else if (last + x < l+1) {
last = last + x;
ans += 2 * y;
}
} printf("Case #%d: %d\n", cas++, ans);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDU 5037 FROG (贪婪)的更多相关文章

  1. hdu 5037 Frog 贪心 dp

    哎,注意细节啊,,,,,,,思维的严密性..... 11699193 2014-09-22 08:46:42 Accepted 5037 796MS 1864K 2204 B G++ czy Frog ...

  2. hdu 5037 Frog(贪心)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5037 题解:为了让放的石头有意义肯定是没l+1的距离放2个也就是说假设现在位置为pos那么 ...

  3. HDU 5037 Frog(贪心)

    题意比较难懂,一只青蛙过河,它最多一次跳L米,现在河中有石头,距离不等,上帝可以往里加石头,青蛙非常聪明,它一定会选择跳的次数最少的路径.问怎么添加石头能让青蛙最多的次数.输出青蛙跳的最多的次数. 考 ...

  4. HDU 5037 Frog(2014年北京网络赛 F 贪心)

    开始就觉得有思路,结果越敲越麻烦...  题意很简单,就是说一个青蛙从0点跳到m点,最多可以跳l的长度,原有石头n个(都仅表示一个点).但是可能跳不过去,所以你是上帝,可以随便在哪儿添加石头,你的策略 ...

  5. hdu 5037 周期优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5037 有只青蛙踩石子过河,河宽m,有n个石子坐标已知.青蛙每次最多跳L.现在可以在河中再放一些石子,使得青蛙过河 ...

  6. hdu 2128 Frog(简单DP)

    Frog Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  7. hdu 5037 模拟网选1006

    /* 模拟 实例: 33 1 10 5 5 2 10 3 3 6 1 3 2 1 1 4 2 1 1 5 2 1 1 6 2 1 1 7 2 1 5 20 8 1 2 3 4 5 1 20 8 5 0 ...

  8. 【数学,方差运用,暴力求解】hdu-5037 Galaxy (2014鞍山现场)

    话说这题读起来真费劲啊,估计很多人做不出来就是因为题读不懂...... 从题目中提取的几点关键点: 题目背景就是银河系(Rho Galaxy)中的星球都是绕着他们的质心(center of mass) ...

  9. hdu 4004 The Frog's Games

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

随机推荐

  1. Codeforces 474 E. Pillars

    水太...... E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Winform WebBrowser引用IE版本问题

    做了一个Winform的项目.项目里使用了WebBrowser控件.以前一直都以为WebBrowser是直接调用的系统自带的IE,IE是呈现出什么样的页面WebBrowser就呈现出什么样的页面.其实 ...

  3. win7已安装Mysql 开机自启动

    1.下载并安装MySql,我用MySQL_5.6.24_winx64_XiaZaiBa,解压缩到磁盘或更低.我在这里安装D菜,D:\install\MySQL\MySQL Server 5.6. 2. ...

  4. 《Linux Device Drivers》第十六章 块设备驱动程序——note

    基本介绍 块设备驱动程序通过主传动固定大小数据的随机访问设备 Linux核心Visual块设备作为基本设备和不同的字符设备类型 Linux块设备驱动程序接口,使块设备最大限度地发挥其效用.一个问题 一 ...

  5. Uncaught TypeError: Cannot read property &#39;call&#39; of undefined jquery.validate.min.js:28

    最近在做表单验证时,,自己写的addMethod 方法总是不起作用.折腾了将近一天. 报告的错误,如下面的 Uncaught TypeError: Cannot read property 'call ...

  6. JS达到Web指定保存的和打印功能的内容

    背景 首先,说说文章的背景.近期手中的一个项目,因为需求中要求提供Web界面的打印功能.当然假设没有打印机,还能够提供保存到本地.项目组长把这个"小任务"分给了我.本着努力为组长分 ...

  7. 第十九章——使用资源调控器管理资源(2)——使用T-SQL配置资源调控器

    原文:第十九章--使用资源调控器管理资源(2)--使用T-SQL配置资源调控器 前言: 在前一章已经演示了如何使用SSMS来配置资源调控器.但是作为DBA,总有需要写脚本的时候,因为它可以重用及扩展. ...

  8. Linux删除以破折号开头的文件Windows在批处理文件来删除隐藏属性

    昨天去打印店打印的材料.结果中毒.所有的文件被隐藏.生成一个一堆快捷键.回来后.我很容易地把它放入Linux机,我想删除这些文件怪. 下面是该过程,遇到的问题. 1.您无法删除'-'该文件的开头 最初 ...

  9. C指针决心 ------ 指针表达式

    本文是自己学习所做笔记.欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020 所谓的指针表达式是指一个表达式.其结果是一个指针. 例1. int  a,b; ...

  10. HDU ACM 1290 献给杭电五十周年校庆的礼物

    解析: 1.n条直线把平面切割成的区域数为: f(n)=f(n-1)+n=n(n+1)/2+1; 2.把空间切割为最多区域数的时候,第n个平面与前(n-1)个平面相交.且无三面共线,因此该平面与前(n ...