Frog - HDU 5037 想法题
来源:互联网 发布:阿里实时数据 编辑:程序博客网 时间:2024/05/17 04:14
Frog
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 413 Accepted Submission(s): 89
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.
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.
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
21 10 552 10 336
Sample Output
Case #1: 2Case #2: 4
思路:贪心的讲,在跳的步数一样多的时候,肯定是此时青蛙的位置越小越好。用pos1和pos2记录青蛙上次跳跃的点和当前的点,如果前面L没有石头的话,就让他们每次跳L+1,这里是可以直接做除法算出来的。所以复杂度是o(n)的。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int val[200010];int main(){ int T,t,n,m,i,j,k,l,ans,pos1,pos2,point,ret; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%d%d%d",&n,&m,&l); for(i=1;i<=n;i++) scanf("%d",&val[i]); if(n>0) sort(val+1,val+1+n); val[0]=0; while(val[n]>=m && n>0) n--; val[n+1]=m; n++; ans=0; pos1=-l-10; pos2=0; point=1; while(pos2<m) { while(val[point]<=pos2 && point<n) point++; if(val[point]-pos2<=l) { while(val[point]-pos2<=l && point<=n) point++; point--; pos1=pos2; pos2=val[point]; ans++; } else { k=(val[point]-pos2)/(l+1); ans+=k*2; if(val[point]-(pos1+(l+1)*k)>l) { pos1=pos1+(l+1)*k; pos2=pos2+(l+1)*k; } else { ret=pos1; pos1=pos2+(l+1)*(k-1); pos2=ret+(l+1)*k; ans--; } } } printf("Case #%d: %d\n",t,ans); }}
0 0
- Frog - HDU 5037 想法题
- Hdu 5037 Frog(2014 ACM/ICPC Asia Regional Beijing Online)[贪心 || 想法]
- hdu 5037 Frog
- hdu 5037 Frog(贪心)
- hdu 5037 Frog(高效)
- hdu 5037 Frog
- HDU 5037 Frog
- hdu 5037 Frog
- hdu 5037 Frog 贪心
- HDU 5037 FROG (贪心)
- hdu 5037 Frog
- HDU 5037 Frog
- hdu 5037 Frog
- hdu 5037 Frog(贪心)
- HDU 5037 Frog
- hdu 5037 Frog 【贪心】
- hdu 5037 Frog(贪心)
- hdu 5037 Frog (贪心)
- 找出数据集合中的最小值和最大值的两种算法比较
- linux crontab
- 深刻理解JavaScript基于原型的面向对象
- VS2010 设置所有工程默认配置路径
- java类打包成jar文件&&jar包的引入
- Frog - HDU 5037 想法题
- STL内存管理
- Android应用程序的Activity启动过程简要介绍和学习计划
- 数组中出现次数超过一半的数
- [MySQL] 生产环境MySQL数据库事务一直在RUNNING
- rtx任务管理
- 2014.09.21 周一-没记录
- HDU 3787
- 第二章顺序表视频中的作业