标签:des style blog http color io os ar java
2 1 10 5 5 2 10 3 3 6
Case #1: 2 Case #2: 4
青蛙过河,河上有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; }
标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/nike0good/article/details/39718171