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 sjm421127 at 2007-10-15 18:48:35 on Problem 1011
#include "iostream"
using namespace std;
int n,a[64],eq;
int mark[64];
////////////////////////////////////////////
//优化方案:
//(1)最大的要在下次中用掉。
//(2)同样的试过不行,下次就可以不用试了。
////////////////////////////////////////////
void search(int min,int level,int pos,int &flag)
{
	if(level==n+1)
	{
		flag=1;
		return ;
	}
	if(!flag)
	{
		int i,last;
		for(i=pos;i>=0;i--)
			if(!mark[i]&&min>=a[i]&&last!=a[i])
			{
				last=a[i];
				mark[i]=1;
				int k=min-a[i];
				if(k)
					search(k,level+1,i,flag);
				else
					search(eq,level+1,n-1,flag);
				mark[i]=0;
			}
	}
}
bool fit(int min)
{
	int flag=0;
	eq=min;
	search(min,1,n-1,flag);
	if(flag)
		return true;
	else 
		return false;
}
void sort()
{
	int i,j;
	for(i=0;i<n;i++)
		for(j=0;j<n-i-1;j++)
			if(a[j]>a[j+1])
			{
				int temp;
				temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
			}
}
int main()
{
	while(cin>>n)
	{
		if(!n)
			break;
		memset(a,0,sizeof(a));
		memset(mark,0,sizeof(mark));
		int i,min,sum=0;
		for(i=0;i<n;i++)
		{
			cin>>a[i];
			sum+=a[i];
		}
		sort();
		min=a[n-1];
		while(sum%min!=0||!fit(min))	
			min++;
		cout<<min<<endl;
	}
	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