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

Posted by zhb_msqx at 2007-08-15 18:56:47 on Problem 1011
#include <iostream>
#include <fstream>
using namespace std;

int snum;
int s[1000];
int mark[1000]; //标志访问位

int find;

int sum;//总和
int partnum; //分割的部分个数
int psum;

int cmp(const void *s,const void *t){
	return (*(int *)t-*(int *)s);
}

void stick(int level);

void mysearch(int level,int cp,int sums){
	if(s[cp]==sums){
		stick(level+1);
	}else{
		int l=sums-s[cp];
		for(int t=cp+1;t<snum;t++){
			if(mark[t]==0&&s[t]<=l){
				mark[t]=1;
				mysearch(level,t,l);
				mark[t]=0;
			}
		}
	}
}

void stick(int level){
//	cout<<level<<endl;
	if(level>partnum){
		find=1;
		return;
	}
	for(int i=0;i<snum;i++){
		if(mark[i]==0){
			mark[i]=1;
			mysearch(level,i,psum);
			mark[i]=0;
		}
	}

}

void perform(){
	for(int k=s[0];k<sum&&find==0;k++){
		if(sum%k==0){	
			partnum=sum/k;
			psum=k;
			stick(1);
		}
	}
}

void main(){
//	ifstream in("data.txt");
	while(1){
		cin>>snum;
		if(snum==0)break;
		else{
			find=0;sum=0;
			memset(s,0,sizeof(s));
			memset(mark,0,sizeof(mark));

			for(int i=0;i<snum;i++){
				cin>>s[i];
				sum+=s[i];
			}
			qsort(s,snum,sizeof(int),cmp);
		//	cout<<s[0]<<endl;
			perform();
			if(find==1)cout<<psum<<endl;
		}
	}
}

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