poj1011 sticks
来源:互联网 发布:java 序列化对比 编辑:程序博客网 时间:2024/05/18 02:47
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
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65
题意简单说就是本来几根一样长的棍子 被小明乱切(乱切 没切中也是有可能的) 切成了n根小棍子
要你把n根小棍子恢复原样 可能有多组解
要求输出 每根棍子最短的情况时 棍子长度
据说是极为经典的dfs题
一开始真的不知道怎么搜好(辣鸡无力)闷了1个多小时无奈去看了一下题解 恍然大悟
看完自己敲一A后 细看一下 还是原来那些东西 dfs传参 之前积累的棍长 棍的数量 累加棍的位置
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <cstdlib>#include <stack>using namespace std;int stick[70];bool flag[70];int len,sum,num,n;//len 棍长 sum所有棍子总长度 num棍数目 n小棍数目bool dfs(int pre_len,int pre_num,int pos){ if(pre_num==num) return true; for(int i=pos;i<n;i++) { if(flag[i]) continue; if(pre_len+stick[i]==len) { flag[i]=true; if(dfs(0,pre_num+1,0)) return true; flag[i]=false;//回溯 return false; } else if(len>(pre_len+stick[i])) { flag[i]=true; if(dfs(pre_len+stick[i],pre_num,i+1)) return true; flag[i]=false; if(pre_len==0) return false;//这时 之前一根棍都没有 len>stick[i] while(i+1<n&&stick[i]==stick[i+1]) i++;//相邻一样的情况就不用算了 } } return false;}int main(){ while(scanf("%d",&n),n) { num=sum=0; for(int i=0;i<n;i++) { scanf("%d",&stick[i]); sum+=stick[i]; } sort(stick,stick+n,greater<int>()); memset(flag,false,sizeof flag); for(len=stick[0];len<=sum;len++) { if(sum%len) continue;//必须整除 num=sum/len;//总共棍子的数量 if(dfs(0,0,0)) { printf("%d\n",len); break; } } } return 0;}
0 0
- poj1011 sticks
- POJ1011 Sticks
- POJ1011 Sticks
- poj1011 - Sticks
- POJ1011-Sticks
- POJ1011 Sticks
- POJ1011 Sticks
- poj1011 (Sticks)
- poj1011 Sticks
- poj1011 Sticks
- poj1011 sticks
- 【poj1011】 Sticks
- POJ1011 Sticks
- poj1011 sticks
- poj1011:Sticks
- poj1011-Sticks
- POJ1011 HDU1455 Sticks
- POJ1011-Sticks DFS+剪枝
- Web Cache替换算法分析(一)
- Modelsim与ISE联合仿真若干须知
- React Native学习笔记(五)Redux基础学习
- Web Cache替换算法分析(二)
- 《Windows程序设计》读书笔九 子窗口控件
- poj1011 sticks
- css3模糊滤镜
- 欢迎使用CSDN-markdown编辑器
- core文件调试
- ibatis中#和$符号的区别
- core文件调试
- 转:JavaScript数组方法总结
- JAVA中的枚举类型
- cache源码分析一 存储机制分析