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 suda1227401034 at 2013-04-27 13:14:42 on Problem 1011
//poj 1011
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

int a[70],n;
bool visit[70];

int cmp(int x,int y)
{
	return x>y;
}

int dfs(int len,int left,int num)
{
	if(left==0&&num==0)
	{
		return len;
	}
	if(left==0)
	{
		left=len;
	}
	for(int i=0;i<n;i++)
	{
		if(!visit[i])
		{
			if(a[i]<=left)
			{
				visit[i]=1;
				if(dfs(len,left-a[i],num-1))
				{
					return len;
				}
				visit[i]=0;
				if(a[i]==left||left==len)
				{
					break;
				}
				for(int j=i+1;j<n;j++)
				{
				   if(a[j]!=a[i])
				  {
					i=j-1;
					break;
				  }
				}
			}
		}
	}
	return 0;
}
int main()
{

	while(scanf("%d",&n)!=EOF&&n)
	{
		int sum=0;
		//memset(a,0,sizeof(a));
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		sort(a,a+n,cmp);

		int k;
		for(int len=a[0];len<=sum;len++)
		{
			if(sum%len==0)
			{
				memset(visit,0,sizeof(visit));
				k=dfs(len,0,n);
				if(k)
				{
					break;
				}
			}
		}
		printf("%d\n",k);
	}
}

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