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. 传智播客成都校园php纪律指控

    继传智播客成都校区php第一期班圆满开班,说明php的火爆一点儿也不亚于java! 经传智播客商讨决定,传智播客成都校区php学科收费标准例如以下: 採用下面不论什么一种方式都能够享受优惠价: 一.自 ...

  2. 2014在百度之星资格赛的第二个问题Disk Schedule

    事实上,我认为它可以用来费用流问题.但光建地图上加班. ..不科学啊.. . 因副作用太大,否则,必然在.最后,想啊想,或者使用dp对.... 别想了一维dp... .我不知道我是怎么想.无论如何,这 ...

  3. 如何与多个线程的操作epoll fd

    自己曾经做一个接口server时候,这样的场景下我的设计是多个线程操作同一个epoll fd.彼时,我的理由是epoll的系列函数是线程安全的. 当然有人不理解为什么会有多个线程操作同一个epoll ...

  4. Linux Kernel(Android) 加密算法汇总(三)-应用程序调用内核加密算法接口

    于Linux Kernel(Android) 加密算法总结(cipher.compress.digest)文章中.介绍了怎样在内核中增加三种不同类型的内核加密算法, 并给出了在内核模块中怎样调用他们的 ...

  5. Android新建项目后src下没有自动生成文件

    最近开始学Android了,按照教材新建了一个项目,发现src下没有自动生成文件,怎么回事呢? 出现这种可能的原因很可能是ADT与SDK版本不同,造成不兼容. 在ADT(或者eclipse)中的hel ...

  6. 【BZOJ1014】【JSOI2008】火星人prefix Splay处理区间,hash+dichotomy(二分)check出解

    题意不赘述了,太清晰了. 说题解:首先依据原字符串建立SPT.首尾建议多加一个空白字符. 给一个树构图,依照平衡树的前后大小顺序性质能够使它们始终维持为一个序列,而且能够通过rank找到序列的第k个. ...

  7. php+sqlite 最佳web服务器

    1 wampserver   支持mysql.每次都启动mysql,可以手动停止.但是运行时有时会很慢. 放弃 2 APS绿色版(Apache+PHP+SQLite)  组件环境:Apache2.2. ...

  8. 【Android进阶】判断网络连接状态并自动界面跳转

    用于判断软件打开时的网络连接状态,若无网络连接,提醒用户跳转到设置界面 /** * 设置在onStart()方法里面,可以在界面每次获得焦点的时候都进行检测 */ @Override protecte ...

  9. 【Android基础】短信的发送

    //Button的点击事件 @Override public void onClick(View v) { // 接受者电话号码 Uri uri = Uri.parse("smsto:123 ...

  10. [改变自己wordpress.2]给wordpress加上简单debug sql调试.

    或者说,同事. 需要帮她打印出来sql 调试输出到页面sql 在这里,我们使用插件或一个的方式来启动配置文件wordpress的debug 在插件文件夹 wordpress/wp-content/pl ...