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 |
高手帮忙看看阿~~~why TLE?????~~~~~~~~~~~~~#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX 70 int stick[MAX],n; bool used[MAX]; bool res,fail; int len, lennum; int cmp(const void *a, const void *b) { return * (int *)b - * (int *)a ; } void Search(int curlen, int num, int next) { int i, tmp, before; //if(res)return; if(fail || res)return; if(curlen == len) { curlen = 0; next = 0; fail = false; ++num; } if(num == lennum) { res = true; return ; } if(next >= n) { fail = true; return; } before = 0; for(i = next; i < n; ++ i) if(!used[i]) { tmp = curlen + stick[i];printf("%d*",tmp); if(tmp <= len) { if(stick[i] == before)continue; used[i] = true; printf("%d ",tmp); Search(tmp, num, i+1); if(res)return; used[i] = false; before = stick[i]; } } } int main () { int i, sum; while(scanf("%d", &n) && n) { sum = 0; for(i = 0; i < n; ++ i) { scanf("%d" ,&stick[i]); sum += stick[i]; } res = false; qsort(stick,n,sizeof(int),cmp); len = stick[0]; while(len < sum) { if(sum % len == 0) { memset(used,0,sizeof(used)); lennum = sum / len; fail = false; Search(0,0,0); if(res) { printf("%d\n", len); break; } } ++len; } if(!res)printf("%d\n", sum); } return 0 ; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator