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 |
永远的TLE,感觉提到的主要剪枝都做了啊,,,最后是借鉴某解题报告修改代码之后过了,但是我自己的这个还是没看出来到底有哪里重复计算了,明明提到的那几个剪枝都做了啊,虽然代码写的有点麻烦。。。我觉得我比AC的代码还多剪了一些的说。。。 ========================================================================= #include<stdio.h> #include<string.h> #include<stdlib.h> int mark[70] = {0}; int len[70]; int n,g,min; int comp(const void *a,const void *b); int find(int no,int lenth,int now,int count); int main() { int i,sum; while(scanf("%d",&n)&&n) { sum = 0; for(i = 0;i < n;i++) { scanf("%d",&len[i]); sum += len[i]; } qsort(len,n,sizeof(int),comp); for(i = len[0];i <=sum;i++) { if((sum%i) == 0) { g = sum/i; min = 0; if(find(0,i,0,0)) { printf("%d\n",i); break; } } } } } int comp(const void *a,const void *b) { return *(int *)b-*(int *)a; } int find(int no,int lenth,int now,int count) { int i,j,f; if((len[no]+now) == lenth) { if(count == (g-2)) return 1; mark[no] = 1; if(no == min) { for(i = no+1;i < n;i++) if(!mark[i]) { min = i; break; } } f = find(min,lenth,0,count+1); if(no < min) min = no; mark[no] = 0; return f; } if((len[no]+now) < lenth) { mark[no] = 1; if(no == min) { for(i = no+1;i < n;i++) if(!mark[i]) { min = i; break; } } for(i = no+1;i < n;i++) { if(!mark[i]&&((len[i]+now+len[no])<lenth)) { f = find(i,lenth,now+len[no],count); mark[no] = 0; if(f == 0) { if(now == 0) return 0; for(j = i+1;j < n;j++) { if(len[j]!=len[i]) { break; } } i = j-1; } else { return 1; } } if(!mark[i]&&((len[i]+now+len[no])==lenth)) { f = find(i,lenth,now+len[no],count); mark[no] = 0; if(no < min) min = no; return f; } } if(no < min) min = no; mark[no] = 0; return 0; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator