首页 > 代码库 > hdu 5037 Frog (贪心)
hdu 5037 Frog (贪心)
Frog
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 789 Accepted Submission(s): 198
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
2 1 10 5 5 2 10 3 3 6
Sample Output
Case #1: 2 Case #2: 4
贪心思想:每次总是尽量让青蛙走最近的石头,需要记录当前和上一个青蛙跳的石头位置。
1、如果可以跳到下一个石头能到达,就跳到最远的那个;
2、否则,就需要添加加石头,石头加在max(now+1,pre+l+1)处,也就是pre刚好不能跳到的,now能跳到的最近的那一个。使前一个和后一个距离总为L+1,得到最优解。
#include"stdio.h" #include"math.h" #include"string.h" #include"iostream" #include"algorithm" using namespace std; #define N 200005 const int inf=(int)1e10; int a[N]; int max(int a,int b) { return a>b?a:b; } int main () { int n,i,cnt=1,T,m,l,ans; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&l); for(i=1;i<=n;i++) scanf("%d",&a[i]); a[0]=0; a[n+1]=m; sort(a,a+n+2); n+=2; int pre,now; pre=-inf; now=0; ans=0; i=1; while(now<m) { while(a[i]-now<=l&&i<n) i++; if(a[i-1]>now&&a[i-1]<=now+l) { pre=now; now=a[i-1]; ans++; } else { int w=(a[i]-now)/(l+1)-1; if(w>0) { int t=max(now+1,pre+l+1); pre=t+(w-1)*(l+1); now+=w*(l+1); ans+=w*2; } else { int t=max(now+1,pre+l+1); pre=now; now=t; ans++; } } } printf("Case #%d: %d\n",cnt++,ans); } return 0; }
hdu 5037 Frog (贪心)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。