标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 126238 | Accepted: 29477 |
Description
Input
Output
Sample Input
9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
Sample Output
6 5
uva上面提交超时,poj上可以过;
AC代码:
1 #include<algorithm> 2 #include<cstring> 3 #include<cstdio> 4 #include<iostream> 5 using namespace std; 6 const int N = 1e4+10; 7 int len[N],sum,L,T; 8 int used[N]; 9 //bool cmp(int x,int y) 10 //{ 11 // if(x>y) return true; 12 //} 13 int cmp(const void *a,const void *b) 14 { 15 return *(int *)b-*(int *)a; 16 } 17 bool DFS(int m,int left)//m为剩余的木棒数,left为当前正在拼接的木棒和假定的木棒长度L还缺少的长度 18 { 19 if(m == 0 && left == 0) 20 return true; 21 if(left == 0)//一根刚刚拼完 22 left = L; 23 for(int i=0; i<T; i++) 24 { 25 if(!used[i] && len[i]<=left) 26 { 27 if(i>0)//如果前者已经出现过的不能用,则当前的也不能用 28 { 29 if(!used[i-1] && len[i] == len[i-1]) 30 continue; 31 } 32 used[i] = 1; 33 if(DFS(m-1,left-len[i])) 34 return true; 35 else 36 { 37 used[i] = 0; 38 if(len[i] == left || left == L) 39 return false; 40 } 41 } 42 } 43 return false; 44 } 45 int main() 46 { 47 while(scanf("%d",&T) && T) 48 { sum = 0 ; 49 for(int i=0;i<T;i++) 50 { 51 scanf("%d",&len[i]); 52 sum = sum + len[i]; 53 } 54 //sort(len,len+T,cmp); sort 超时 55 qsort(len,T,sizeof(int),cmp); //从大到小排序 56 for(L = len[0];L<=sum/2;L++) 57 { 58 if(sum%L) continue; 59 memset(used,0,sizeof(used)); 60 if(DFS(T,L)) 61 { 62 printf("%d\n",L); 63 break; 64 } 65 } 66 if(L>sum/2) printf("%d\n",sum); 67 } 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/lovychen/p/4478172.html