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 |
终于AC了!!!!!!!!!!!!!!!!!!DPS!!!!!!!!!!!!!!#include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> int s[65],sum=0,Maylen,num,n; bool used[65]; int cmp(const void* a,const void* b) { return *(int*)b-*(int*)a; } bool dfs(int sticks,int curlen,int pos) { int i; if(sticks==num) return true; for(i=pos+1;i<n;i++) { if(used[i]) continue; if(curlen+s[i]==Maylen) { used[i]=true; if(dfs(sticks+1,0,-1)) return true; used[i]=false; return false; } else if(curlen+s[i]<Maylen) { used[i]=true; if(dfs(sticks,curlen+s[i],i)) return true; used[i]=false; if(!curlen) return false; while(s[i]==s[i+1]) i++; } } return false; } using namespace std; int main() { while(scanf("%d",&n)==1) { if(!n) break; sum=0; for(int i=0;i<n;i++) { cin>>s[i]; sum+=s[i]; } qsort(s,n,sizeof(int),cmp); for(Maylen=s[0];Maylen<=sum;Maylen++) { if(sum%Maylen) continue; else num=sum/Maylen; memset(used,false,sizeof(used)); if(dfs(1,0,-1)) { cout<<Maylen<<endl; break; } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator