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 |
感觉自己的程序好慢啊!!!//poj 1011 #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; int a[70],n; bool visit[70]; int cmp(int x,int y) { return x>y; } int dfs(int len,int left,int num) { if(left==0&&num==0) { return len; } if(left==0) { left=len; } for(int i=0;i<n;i++) { if(!visit[i]) { if(a[i]<=left) { visit[i]=1; if(dfs(len,left-a[i],num-1)) { return len; } visit[i]=0; if(a[i]==left||left==len) { break; } for(int j=i+1;j<n;j++) { if(a[j]!=a[i]) { i=j-1; break; } } } } } return 0; } int main() { while(scanf("%d",&n)!=EOF&&n) { int sum=0; //memset(a,0,sizeof(a)); for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } sort(a,a+n,cmp); int k; for(int len=a[0];len<=sum;len++) { if(sum%len==0) { memset(visit,0,sizeof(visit)); k=dfs(len,0,n); if(k) { break; } } } printf("%d\n",k); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator