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

高手帮忙看看阿~~~why TLE?????~~~~~~~~~~~~~

Posted by never_gone at 2007-04-21 14:20:06 on Problem 1011
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 70

int stick[MAX],n;
bool used[MAX];
bool res,fail;
int len, lennum;


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

void Search(int curlen, int num, int next)
{
	int i, tmp, before;
    
    //if(res)return;
    
    if(fail || res)return;	
    
	if(curlen == len)
	{
		curlen = 0;
		next = 0;
		fail = false;
		++num;
	}	
	if(num == lennum)
	{
		res = true;
		return ;
	}
	if(next >= n)
	{
		fail = true;
		return;
	}
	before = 0;
	for(i = next; i < n; ++ i)
	    if(!used[i])
		{
			tmp = curlen + stick[i];printf("%d*",tmp);
			if(tmp <= len)
			{
				if(stick[i] == before)continue;
				used[i] = true;	printf("%d ",tmp);		
				Search(tmp, num, i+1);						
				if(res)return;
				used[i] = false;
				before = stick[i];
			}
		}
}

int main ()
{
	int i, sum;
	while(scanf("%d", &n) && n)
	{
		sum = 0;
		for(i = 0; i < n; ++ i)
		{
			scanf("%d" ,&stick[i]);
			sum += stick[i];		
		}
		res = false;
		qsort(stick,n,sizeof(int),cmp);
		len = stick[0];
		while(len < sum)
		{
			if(sum % len == 0)
			{
				memset(used,0,sizeof(used));
				lennum = sum / len;
				fail = false;
				Search(0,0,0);
				if(res)
				{
					printf("%d\n", len);
					break;
				}
			}
			++len;
		}
		if(!res)printf("%d\n", sum);
	}
	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