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 |
Re:这样剪不对呀In Reply To:这样剪不对呀 Posted by:wanglei at 2004-03-26 11:13:13 #include <stdio.h> #include <string.h> #include <algorithm> int c[101]; bool d[101]; int n,p; int sum,sticks; int flag; bool cmp(int a,int b) { return a>b; } void create(int q,int use,int k) { if (flag) return ; int i; if (!q) { if (use==sticks-1) { flag=1; printf("%d\n",p); return ; } for (i=0;i<n;i++) if (!d[i]) break; d[i]=1; create(p-c[i],use+1,i); d[i]=0; return ; } if (k+1>=n) return ; if (c[k+1]==0) return ; if (k>0&&!d[k-1]&&c[k-1]==c[k]) return ; for (i=k+1;i<n;i++) if (!d[i]&&q>=c[i]) { d[i]=1; create(q-c[i],use,i); d[i]=0; if (flag) return ; if (c[i]==q) return ; } } int main() { while (scanf("%d",&n)==1) { if (n==0) break; sum=p=0; for (int i=0;i<n;i++) { scanf("%d",&c[i]); if (c[i]<=50) { sum+=c[i]; if (c[i]>p) p=c[i]; } else c[i]=0; } std::sort(c,c+n,cmp); flag=0; if (p==0) {printf("0\n"); continue;} for (;p<=sum;p++) if (sum%p==0) { memset(d,0,n); for (int j=0;j<n;j++) if (!c[j]) d[j]=1; sticks=sum/p; create(0,-1,0); if (flag) 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