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 |
好混乱#include <iostream> using namespace std; int n; int *a; int sum; int *used; bool dfs(int start,int sum,int len); bool can(int len); void sort() { for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) { if(a[i]<a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } int getsum() { int sum = 0; for(int i=0;i<n;i++) sum += a[i]; return sum; } bool dfs(int begin,int now,int sum,int len) { used[begin] = 1; if(sum>len||now==n-1&&sum!=len) { return false; } if(sum==len) { int start = -1; for(int i=0;i<n;i++) if(!used[i]) { start = i; break; } if(start!=-1) { return dfs(start,start,a[start],len); } if(start==-1) { return true; } } bool temp = true; int last; for(int i=now+1;i<n;i++) { if(used[i]) continue; if(temp==false&&a[i]==a[last]) continue; used[i] = 1; temp = dfs(begin,i,sum+a[i],len); last = i; if(temp==true) return true; used[i] = 0; } used[begin] = 0; return false; } int getMin() { for(int i=a[0];i<=sum/2;i++) { if(sum%i!=0) continue; for(int j=0;j<n;j++) used[j] = 0; if(dfs(0,0,a[0],i)) return i; } return sum; } int main() { while(1) { cin>>n; a = new int[n]; used = new int[n]; if(n==0) break; for(int i=0;i<n;i++) { cin>>*(a+i); used[i] = 0; } sum = getsum(); sort(); cout<<getMin()<<endl; delete a; delete used; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator