码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5037(Frog-贪心青蛙跳石子)

时间:2014-10-01 17:57:31      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   java   

Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1596    Accepted Submission(s): 442


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
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5057 5056 5055 5054 5053 

青蛙过河,河上有n枚石子,青蛙想从点0跳到点m,一次最多跳l个单位长度,你可以任意往上摆石子,使青蛙采取最优策略时跳得次数最多,问:青蛙跳得最多次数。


显然:

1.青蛙跳得永远是最远的石子。

2.在青蛙能跳过去时不加石子最优。

考虑青蛙在没石子时,从0跳到t(l+1)+x

显然青蛙向前跳得第2个石子至少与它距离为(l+1)

故这么跳不劣

0,1,l+1,(l+1)+1,2(l+1),2(l+1)+1,...t(l+1),t(l+1)+x.  //第x项与第x+2项距离为l+1,最优

那么当青蛙从pre(负数)跳到0,再跳到t(l+1)+x时,额外保证pre开始的第二颗石与它距离>=l+1

0,pre+l+1,l+1,pre+2(l+1),2(l+1)...t(l+1),t(l+1)+x


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (2*100000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int T,n,m,l,a[MAXN];
bool avi(int a,int b){return a+l>=b;}
int main()
{
//	freopen("hdu5037.in","r",stdin);
//	freopen("hdu5037.out","w",stdout);
	scanf("%d",&T);
	For(t,T)
	{
		scanf("%d%d%d",&n,&m,&l); //total_rocks length max_step
		For(i,n) scanf("%d",&a[i]);
		sort(a+1,a+1+n);a[++n]=m;a[n+1]=INF;
		int now=0,pre=-l,ans=0; //当pre=-l,now=0时 pre+l+1=now+1 
		For(i,n)
		{
			if (a[i+1]<=now+l) continue; //跳到更前面 
			if (a[i]<=now+l) {pre=now;now=a[i];ans++;}
			else 
			{
				int t=(a[i]-now)/(l+1),x=(a[i]-now)%(l+1);
				pre+=t*(l+1); ans+=2*t;
				if(!avi(pre,a[i])) 
				{
					now+=t*(l+1);
					if(i<n&&avi(now,a[i+1])) continue;
					else pre=now,now=a[i],ans++; 
				}
				else 
				{
					int k=now-pre; now=pre;pre-=k;ans--;
					
					if(i<n&&avi(now,a[i+1])) continue;
					else pre=now,now=a[i],ans++; 
					
				}
			}
			
			
		}
		
		printf("Case #%d: %d\n",t,ans);
	}
	
	
	return 0;
}











HDU 5037(Frog-贪心青蛙跳石子)

标签:des   style   blog   http   color   io   os   ar   java   

原文地址:http://blog.csdn.net/nike0good/article/details/39718171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!