POJ-1011(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
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5
分析:
- 先存大的棍子,对于每一次搜索,关心的状态有三个:现在拼的棍子是第几个,已经拼了多少了,上次拼的那根是序列中的第几根
- 如果拼当前棍子的时候,拼了一个长度为a的棍子,发现失败了,那么之后就不用再尝试长度为a的棍子了
- 如果现在要拼的棍子长度为0,然后发现有一个棍子试图去拼然后失败了,那么这条路子就是错的,因为最终肯定不会全部拼完整,
- 同理,如果刚好拼好一根棍子,发现接下来再拼失败了,那么接下来也没办法成功拼完整的。
int a[100],v[100],n,cnt,len;
bool cmp(int a,int b)
{
return a>b;
}
bool dfs(int stick,int cab,int last)
{
if(stick == cnt+1)
return true;
if(cab == len)
return dfs(stick+1,0,1);//这里第一次忘记return了,好坑
int fail = 0;
for(int i=last;i<=n;i++)
{
if(!v[i]&&cab+a[i]<=len&&fail!=a[i])
{
v[i] = 1; if(dfs(stick,cab+a[i],i+1))
return true;
fail = a[i];
v[i] = 0;
//如上述最后两条所说
if(cab==0||cab+a[i]==len)
return false;
}
}
return false;
}
int main()
{
while(cin>>n,n)
{
int sum = 0,val = 0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
val = max(val,a[i]);
}
sort(a+1,a+1+n,cmp);
for(len = val;len<=sum;len++)
{
if(sum%len)continue;
cnt = sum/len;
//cout<<cnt<<endl;
memset(v,0,sizeof v);
if(dfs(1,0,1))break;
}
cout<<len<<endl;
}
return 0;
}
POJ-1011(sticks,深搜)的更多相关文章
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- POJ 1011 Sticks(dfs+剪枝)
http://poj.org/problem?id=1011 题意:若干个相同长度的棍子被剪成若干长度的小棍,求每根棍子原来的可能最小长度. 思路:很经典的搜索题. 我一开始各种超时,这题需要很多剪枝 ...
- OpenJudge 2817:木棒 / Poj 1011 Sticks
1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...
- POJ 1011 Sticks 【DFS 剪枝】
题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- POJ 1011 Sticks(搜索 && 剪枝 && 经典)
题意 : 有n根木棍(n<=64),它们由一些相同长度的木棍切割而来,给定这n根木棍的长度,求使得原来长度可能的最小值. 分析 : 很经典的深搜题目,我们发现答案只可能是所有木棍长度总和的因数, ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
- poj 1011 Sticks
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 126238 Accepted: 29477 Descrip ...
随机推荐
- iOS后台模式BackgroundMode
概述 iOS平台提供给应用特殊的后台服务,应用在后台时仍被允许能执行一会. 后台模式 UIBackgroundModes Xcode Modes 1. audio -> Audio.Airpla ...
- Codeforces 711B 【模拟】
比赛的时候绝壁打麻烦了... 考虑的好麻烦...wa7...还要判断出来的是不是positive的... 好吧..认了.. #include<cstdio> #include <ma ...
- 基于FBX SDK的FBX模型解析与加载 -(三)
http://blog.csdn.net/bugrunner/article/details/7229416 6. 加载Camera和Light 在FBX模型中除了几何数据外较为常用的信息可能就是Ca ...
- template code 引用的一些问题
1.问题: 引用同一个norlib.tt 下面的tt . 一个KSTrade 正确. 一个 NDAP就报错. 报错说源文件某个函数有错误 helper.Common.tt 错误 2.结果: NDAP ...
- 不常见偏门的Bug,Spring Boot IDEA 静态资源 图片访问404,初学者之殇
用过Idea朋友都知道,它有一个非常让人喜欢的功能就是:打算在某个a目录下创建一个hello.class文件,那么你仅需要右键点击New-Java Class- 然后输入名字:a.hello 即可. ...
- iOS 将WKWebView内的HTML打印为PDF
使用的webview为WKWebView,核心部分代码(Swift 4): // 创建打印渲染 let printPageRenderer:PDFRender = PDFRender() // 获取渲 ...
- charles之抓包和断点
一 .charles抓包 Charles抓包很简单,只要手机设置代理即可,不会的也可以去百度. 在这里是要记录抓包过程中win10遇到的问题,手机代理设置没问题但是就是抓不到包的情况 1.关闭防火墙 ...
- 题解报告:hdu 6440 Dream(费马小定理+构造)
解题思路:给定素数p,定义p内封闭的加法和乘法运算(运算封闭的定义:若从某个非空数集中任选两个元素(同一元素可重复选出),选出的这两个元素通过某种(或几种)运算后的得数仍是该数集中的元素,那么,就说该 ...
- python_10(模块与包)
第1章 模块 1.1 模块的种类 1.2 定义 1.3 作用 1.4 导入及使用 1.4.1 import 1.4.2 测试一: 1.4.3 测试二: 1.4.4 测试三: 1.4.5 小结 1.4. ...
- Dapper系列之二:Dapper的事务查询
Dapepr讲解 上篇文章我们介绍了,什么是Dapepr,有什么好处,性能的对比,还有多表多数据添加操作(事务的封装)等等.本篇文章我们继续讲解.....如果本篇文章看不懂,请看我上一篇文章:Dape ...