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

TLE,谁给个好算法啊,谢谢啦!!!

Posted by 1987123_abc at 2006-09-10 16:16:32 on Problem 3003
#include "stdio.h"
int besth,ch,bh,d[41],N,M,flag,r;
char p[41][2];
void backtrack(int i)
	{int j;
	if(i>M) 
		{if(ch==0)
			{if(bh+2<besth) 
				{besth=bh+2;
				 for(j=1;j<=M;j++)
					p[j][1]=p[j][0];
				}
			flag=1;
			}
		return;
		}
	if(r<ch) return;
	r-=d[i];
	if(ch>=d[i])
		{ch-=d[i];
		 p[i][0]='D';		
		 backtrack(i+1);
		 ch+=d[i];
		}	
	if(ch+d[i]<=besth)
		{int mh=bh;
		 if(ch+d[i]>bh) bh=ch+d[i];
		 ch+=d[i];
		 p[i][0]='U';
		 backtrack(i+1);
		 bh=mh;
		 ch-=d[i];
		}
	r+=d[i];
}
int main(int argc, char* argv[])
{int i;
 scanf("%d",&N);
 while(N--)
	{scanf("%d",&M);
	for(i=1;i<=M;i++) scanf("%d",&d[i]);
	r=0;
	for(i=1;i<=M;i++) r+=d[i]; 
	besth=1000;ch=0;bh=0;flag=0;
	backtrack(1);
	if(flag==1) for(i=1;i<=M;i++) printf("%c",p[i][1]);
	else printf("IMPOSSIBLE");
	printf("\n");
	}
	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