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 zwjowen at 2010-04-13 15:42:22 on Problem 1011
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

int n;
int untils[64];
int all;
bool taken[64];
bool search(int start,int sum,int goal,int rest){
	if(rest==0)
		return true;
	if(sum==goal)
	{
		return search(n-1,0,goal,rest-goal);
	}
	int i=start;
	for(;i>=0;i--){
		if(taken[i])
			continue;
		if(sum+untils[i] > goal)
			continue;
		taken[i]=true;
		bool answer=search(i-1,sum+untils[i],goal,rest);
		if(answer)
				return true;
		taken[i]=false;
	}
	return false;
}
int main()
{
	cin>>n;
	while(n!=0){
		all=0;
		for(int i=0;i<n;i++){
			cin>>untils[i];
			all+=untils[i];
		}
		sort(untils,untils+n);
		for(int k=untils[n-1];k<=all;k++){
			if(all%k==0){
				memset(taken,0,sizeof(taken));
				if(search(n-1,0,k,all)){
					cout<<k<<endl;
					break;
				}
			}
		}
		cin>>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