POJ1011 Sticks
2012-05-31 11:39
375 查看
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
Source
Central Europe 1995
//搜索+剪枝
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 72127 | Accepted: 15839 |
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
Source
Central Europe 1995
//搜索+剪枝
#include <cstdlib> #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <cmath> using namespace std; int data[66]; bool tag[65]; int n; bool dfs(int length,int left_length,int left_sum) { if((left_sum==0)&&(left_length==0)) return true; if(left_length==0) left_length=length; if(left_length>left_sum) return false; int i,j,k; for(i=n;i>=1;i--) { if(tag[i]) continue; if(left_length<data[i]) continue; if((i!=n)&&(data[i]==data[i+1])&&!tag[i+1]) continue; tag[i]=true; if(dfs(length,left_length-data[i],left_sum-data[i])) return true; tag[i]=false; if(data[i]==left_length||left_length==length) return false; } return false; } int main(int argc, char *argv[]) {//freopen("C:/Users/shp/Desktop/in.txt","r",stdin); //freopen("C:/Users/shp/Desktop/out.txt","w",stdout); int i,j,k; int sum=0; while(scanf("%d",&n)!=EOF) {if(n==0)break; sum=0; int maxlength=0; for(i=1;i<=n;i++) {scanf("%d",&data[i]);sum+=data[i]; if(data[i]>maxlength) maxlength=data[i]; } memset(tag,false,sizeof(tag)); sort(data+1,data+n+1); for(i=maxlength;i<=sum;i++) { if(sum%i!=0) continue; if(dfs(i,i,sum)) {cout<<i<<endl;break;} } } system("PAUSE"); return EXIT_SUCCESS; }
相关文章推荐
- UVA 307 POJ 1011 Sticks
- poj 1011 Sticks
- POJ 1011--Sticks
- poj 1011 hdoj 1455 Sticks(搜索+剪枝)
- POJ 1011 Sticks ( 深搜、剪枝)
- POJ 1011 Sticks
- POJ 1011 Sticks
- poj 1011 Sticks
- poj 1011 sticks 经典剪枝问题
- POJ 1011 Sticks【dfs】
- POJ 1011 Sticks
- The start—— POJ 1011 Sticks
- poj(1011)——Sticks(经典的dfs+剪枝)
- POJ-1011-Sticks
- POJ 1011:Sticks 经典搜索
- poj1011——Sticks
- POJ 1011 Sticks
- Poj1011 Sticks 小木棍dfs搜索
- 【北大夏令营笔记-深度优先搜索】POJ1011-Sticks
- POJ 1011 Sticks