标签:false 输入 long using int sub current dom rac
9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
6 5
1 #include <bits/stdc++.h> 2 using namespace std; 3 int n,l; 4 int lastN=-1; 5 vector <int> length; 6 int visit[1010]; 7 bool cmp(int a,int b) { 8 return a>b; 9 } 10 bool dfs(int r,int k) { 11 if(r==0&&k==0)return true; 12 if(k==0)k=l; 13 int startN=0; 14 if(k!=l)startN=lastN+1; 15 for(int i=startN; i<n; i++) { 16 if(!visit[i]&&length[i]<=k) { 17 if(i>0) { 18 if(visit[i-1]==false&&length[i]==length[i-1])continue; 19 } 20 visit[i]=1; 21 lastN=i; 22 if(dfs(r-1,k-length[i]))return true; 23 else { 24 visit[i]=0; 25 if(length[i]==k||k==l)return false; 26 } 27 } 28 } 29 return false; 30 } 31 int main() { 32 int c,maxl=0; 33 while(cin>>n&&n!=0) { 34 maxl=0; 35 length.clear(); 36 for(int i=0; i<n; i++) { 37 cin>>c; 38 length.push_back(c); 39 maxl+=length[i]; 40 } 41 sort(length.begin(),length.end(),cmp); 42 for(l=length[0]; l<=maxl/2; l++) { 43 if(maxl%l)continue; 44 memset(visit,0,sizeof(visit)); 45 if(dfs(n,l)) { 46 cout<<l<<endl; 47 break; 48 } 49 50 } 51 if(l>maxl/2)cout<<maxl<<endl; 52 } 53 54 return 0; 55 }
标签:false 输入 long using int sub current dom rac
原文地址:https://www.cnblogs.com/aiqinger/p/12618502.html