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 |
大神们有空给看看代码,dfs 测试数据都过。WA了三次。。。。。。#include<iostream> #include<algorithm> using namespace std; bool dfs(int a[],int b[],int be,int end,int key) { if(!b[be] && key==a[be]) { b[be]=1; return true; } int i,sum=0; for(i=be;i<=end;i++) { if(!b[i]) sum+=a[i]; } if(sum<key) return false; else { int k=be; while(k<=end) { if(!b[k] && a[k]<=key) { if(a[k]==key) { b[k]=1; return true; } if(dfs(a,b,k+1,end,key-a[k])) { b[k]=1; return true; } else { while(a[k]==a[k+1]) k++; } } k++; } if(k>end) return false; } return false; } bool cmp(int a,int b) { return a>b ?1:0; } int main() { int n,i,sum,max,k; int a[65]={0}; int b[65]={0}; bool finished=false; cin>>n; while(n) { memset(a,0,sizeof(a)); sum=0; max=-1; finished=false; for(i=0;i<n;i++) { cin>>a[i]; sum+=a[i]; if(a[i]>max) max=a[i]; } sort(a,a+n,cmp); while(!finished) { if(sum%max==0) { k=0; memset(b,0,sizeof(b)); for(i=1;i<=sum/max;i++) { if(dfs(a,b,0,n-1,max)) k=0; else { k=1; break; } } if(!k) { finished=1; cout<<max<<endl; } } max++; } cin>>n; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator