POJ1011 (DFS+剪枝)
2015-08-29 15:04
246 查看
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: 129606 | Accepted: 30388 |
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
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[70], n; bool vis[70]; int Cmp(int a, int b) { return a>b; } //len 是要构造的每段的长度。 cur是构造每段 //长度的过程中还需要的。 num是剩余的木棒数。 int dfs(int len, int cur, int num) { if(cur==0&&num==0) return true; if(cur==0) cur = len; for(int i=0; i<n; i++) { if(vis[i]) continue; if(cur-a[i]>=0) { vis[i] = 1; if(dfs(len, cur-a[i], num-1)) return true; vis[i] = 0; if(a[i]==cur||cur==len) return false;//a[i]==cur //满足一个木棒长度, 但是不满足全部的 cur==len是进行循环之后还是不满足 while(a[i]==a[i+1]&&i+1<n) ++i; } } return false; } int main() { while(scanf("%d", &n), n) { int sum = 0; for(int i=0; i<n; i++) { scanf("%d", &a[i]); sum+=a[i]; } sort(a, a+n, Cmp); for(int i=a[0]; i<=sum; i++) { if(sum%i==0) { memset(vis, 0, sizeof(vis)); if(dfs(i, 0, n)) { printf("%d\n", i); break; } } } } return 0; }
相关文章推荐
- 简单的.html文件
- Word查找替换栏代码和通配符一览表
- mariadb安装手顺
- A、B、C、D和E类IP地址
- 闰年
- 软件测试
- Yarn通信过程
- Linux下的压缩解压缩命令详解
- solaris下安装pkg.bz2文件
- noip2003 传染病控制 (深搜)
- hdu 1599 find the mincost route
- 【转载】区间信息的维护与查询(一)——二叉索引树(Fenwick树、树状数组)
- 769 乘数密码【字符串】
- PCIE协议解析 synopsys IP Register配置空间 读书笔记(8)
- ZOJ 3557-How Many Sets II(Lucas定理+插板法求组合数)
- [转] linux 信号量之SIGNAL
- linux运行、关闭、查看后台任务
- android平台led开发之内核硬件驱动层
- 题目1386:旋转数组的最小数字 剑指offer08
- udig二次开发 eclipse环境搭建