Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
高手们帮忙看看,总是TLE#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator