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. 使用MySQL Workbench建立数据库,建立新的表,向表中添加数据

    使用MySQL Workbench建立数据库,建立新的表,向表中添加数据 初学数据库,记录一下所学的知识.我用的MySQL数据库,使用MySQL Workbench管理.下面简单介绍一下如何使用MyS ...

  2. hdu 5091 Beam Cannon(扫描线段树)

    题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,如今要有一个W∗H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,如果有点(x,y),那么(x,y)~(x+W,y+ ...

  3. IBatis.Net获取执行的Sql语句

    前言 IBatis.Net中Sql语句是些在配置文件中的,而且配置文件是在程序启动时读取的(我们开发的时候需要将其设置成较新复制或者是始终复制),而不是程序将其包含在其中(例如NHibernate的映 ...

  4. java nio 网络框架实现(转)

    maven项目https://github.com/solq360/common 链式编/解码 链路层链式处理 管道管理socket 多协议处理非常方便 仿netty NioEventLoop 单线程 ...

  5. 探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据

    上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...

  6. [原创].NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇)

    原文:[原创].NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇) .NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇) 前言:接着上篇来. 系列文章链接: [ ...

  7. Swift伟大的编程语言数据采集

    Swift 2048 https://github.com/austinzheng/swift-2048 苹果官方Swift文档<The Swift Programming Language&g ...

  8. OpenCV2学习笔记(十四):基于OpenCV卡通图片处理

    得知OpenCV有一段时间.除了研究的各种算法的内容.除了从备用,据导游书籍和资料,尝试结合链接的图像处理算法和日常生活,第一桌面上(随着摄像头)完成了一系列的视频流处理功能.开发平台Qt5.3.2+ ...

  9. OSChina 的URL类的源代码重写过程

    此代码是 oschina 到手柄形状像 http://www.oschina.net/p/tomcat 这种URL 此类已经废弃,改用 http://www.oschina.net/code/snip ...

  10. struts1吊牌&lt;logic:iterate&gt;

    <logic:iterate>主要用于处理网页上的输出集合,集合是其中一般下列之一: 1. java对象的数组 2. ArrayList.Vector.HashMap等 具体使用方法请參考 ...