Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

终于过了。。。。。我怎么理解地这么慢呢。。。

Posted by boycott at 2009-02-16 16:27:54 on Problem 1037
//up[i][j]表示:长度为i的fence以第j高的木条开始,使前两根木条呈上升的fence总数
//down[i][j]同理。

#include<stdio.h>
#define GetMax(a,b) ((a)>(b)?(a):(b))

int k,i,j,maxn;
__int64 up[21][21],down[21][21],c[101],n[101];

void generate(int n,__int64 c)
{
	int i,j,find,fup,s[21];
	for(i=1;i<=n;++i) s[i]=i;
	for(i=n;i>=1;--i){
		if(i==n){//第一根木条单独处理,因为可能是上升也可能是下降的
			for(j=1;j<=i;++j){
				if(c-down[i][j]<=0) {find=j;fup=1;break;}
				else c-=down[i][j];
				if(c-up[i][j]<=0) {find=j;fup=0;break;}
				else c-=up[i][j];
			}
			printf("%d ",s[find]);
			for(j=find;j<=i;++j) s[j]=s[j+1];
		}
		else
			if(fup==1){
				for(j=1;j<find;++j)
					if(c-up[i][j]<=0) {find=j;fup=0;break;}
					else c-=up[i][j];
				printf("%d ",s[find]);
				for(j=find;j<=i;++j) s[j]=s[j+1];
			}
			else{
				for(j=find;j<=i;++j)
					if(c-down[i][j]<=0) {find=j;fup=1;break;}
					else c-=down[i][j];
				printf("%d ",s[find]);
				for(j=find;j<=i;++j) s[j]=s[j+1];
			}
	}
	printf("\n");
}

int main() 
{ 
	scanf("%d",&k);
	maxn=0;
	for(i=1;i<=k;++i){
		scanf("%d %I64d",&n[i],&c[i]);
		maxn=GetMax(maxn,n[i]);
	}
	
	//DP
	up[1][1]=down[1][1]=1;
	for(i=2;i<=maxn;++i){
		for(j=2;j<=i;++j)
			down[i][j]=down[i][j-1]+up[i-1][j-1];
		for(j=1;j<=i;++j) up[i][j]=down[i][i-j+1];
	}

	for(i=1;i<=k;++i) generate(n[i],c[i]);
	return 0; 
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator