poj 1011 Sticks
2015-05-05 08:32
316 查看
Sticks
Description
George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.
Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
Output
The output should contains the smallest possible length of original sticks, one per line.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 126238 | Accepted: 29477 |
George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.
Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
Output
The output should contains the smallest possible length of original sticks, one per line.
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代码:
#include<algorithm> #include<cstring> #include<cstdio> #include<iostream> using namespace std; const int N = 1e4+10; int len ,sum,L,T; int used ; //bool cmp(int x,int y) //{ // if(x>y) return true; //} int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } bool DFS(int m,int left)//m为剩余的木棒数,left为当前正在拼接的木棒和假定的木棒长度L还缺少的长度 { if(m == 0 && left == 0) return true; if(left == 0)//一根刚刚拼完 left = L; for(int i=0; i<T; i++) { if(!used[i] && len[i]<=left) { if(i>0)//如果前者已经出现过的不能用,则当前的也不能用 { if(!used[i-1] && len[i] == len[i-1]) continue; } used[i] = 1; if(DFS(m-1,left-len[i])) return true; else { used[i] = 0; if(len[i] == left || left == L) return false; } } } return false; } int main() { while(scanf("%d",&T) && T) { sum = 0 ; for(int i=0;i<T;i++) { scanf("%d",&len[i]); sum = sum + len[i]; } //sort(len,len+T,cmp); sort 超时 qsort(len,T,sizeof(int),cmp); //从大到小排序 for(L = len[0];L<=sum/2;L++) { if(sum%L) continue; memset(used,0,sizeof(used)); if(DFS(T,L)) { printf("%d\n",L); break; } } if(L>sum/2) printf("%d\n",sum); } return 0; }
相关文章推荐
- ACM poj 1011 Sticks解题报告源代码【转】
- POJ 1011 Sticks
- poj 1011 Sticks 经典的深搜(dfs)+ 强剪枝
- POJ 1011 Sticks
- POJ 1011 Sticks(dfs+剪枝)
- POJ 1011 Sticks
- poj 1011 sticks
- POJ 1011 Sticks(dfs)
- POJ 1011 Sticks(DFS + 剪枝)
- poj 1011 Sticks
- 【DFS迭代加深搜索+剪枝】POJ_1011_Sticks
- POJ1011 Sticks 深度优先搜索+剪枝
- POJ 1011 Sticks
- 【DFS搜索】poj1011 Sticks
- DFS(剪枝) POJ 1011 Sticks
- POJ 1011 Sticks 经典dfs + 剪枝
- poj--1011sticks
- poj 1011 Sticks
- poj - 1011 Sticks
- POJ1011 Sticks【DFS+剪枝】