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的不行了,论坛上的结果都对,就是TLE

Posted by baidu85 at 2008-04-18 15:12:00 on Problem 1011
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int n;
int stick[65];
int total;
int ns;
int ok;
int len;
int used[65];

int cmp(const void* a,const void* b)  
{
	return(*(int*)b-*(int*)a);
}

void dfs(int num,int now,int next)
{
	if(num==ns)
	{
		ok=1;
		printf("%d\n",len);
		return;
	}
	if(ok)
		return;
	if(next+1>n)
		return;
	else if(!ok)
	{
		int i;
		for(i=next;i<n&&!ok;i++)
		{
			if(!used[i])
			{
				if(now+stick[i]<len)
				{
					used[i]=1;
					dfs(num,now+stick[i],i+1);
					used[i]=0;
				}
				else if(now+stick[i]==len)
				{
					used[i]=1;
					dfs(num+1,0,1);
					used[i]=0;
					break;
				}
			}
		}
	}
	return;
}

int main()
{
	freopen("in.txt","r",stdin);
	while(scanf("%d",&n)==1)
	{
		if(!n)
			break;
		ok=0;
		int i;
		for(i=1;i<=n;i++)
			scanf("%d",&stick[i]);
		qsort(stick+1,n,sizeof(int),cmp);
		total=0;
		for(i=1;i<=n;i++)
			total=total+stick[i];
		for(i=stick[1];i<=total;i++)
			if(total%i==0&&!ok)
			{
				ns=total/i;
				memset(used,0,sizeof(used));
				len=i;
				dfs(1,0,1);
			}
	}
	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