题目链接:http://poj.org/problem?id=1011

Sticks

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 154895   Accepted: 37034

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

Source

题意概括:

George这家伙一开始把几条长度一样的木棍切成了好多小木棍,后来忘记原来有几条长度一样的木棍了,所以请我们帮帮忙把这些小木棍拼成尽可能长的长度相同的长木棍,输出长度。

解题思路:

先根据小木棍的长度降序排序

枚举木棍长度(可以整除小木棍总长度),DFS凑这些木棍。

剪枝:如果当前这条小木棍不能凑则这一类小木棍都凑不了(这也是要排序的原因)

AC code:

 ///poj 1011 dfs
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = ;
bool vis[MAXN];
int sticks[MAXN];
int N, sum, len;
bool flag; bool cmp(int x, int y)
{
return x>y;
} void dfs(int dep, int now_len, int st) ///当前已经使用的木条数目,当前木条的长度, 需要处理的木条的下标
{
if(flag) return;
if(now_len == ) ///找木棍头
{
int k = ;
while(vis[k]) k++;
vis[k] = true;
dfs(dep+, sticks[k], k+);
vis[k] = false;
return;
}
if(now_len == len)
{
if(dep == N) flag = true; ///找到满足条件的len了
else dfs(dep, , ); ///没有用完,开始凑新的一根长度为len的木棍
return;
}
for(int i = st; i < N; i++)
{
if(!vis[i] && now_len + sticks[i] <= len)
{
if(!vis[i-] && (sticks[i-] == sticks[i])) continue;
vis[i] = true;
dfs(dep+, now_len+sticks[i], i+);
if(flag) return;
vis[i] = false;
}
}
} void init()
{
memset(vis, , sizeof(vis));
flag = false;
sum = ;
} int main()
{
while(~scanf("%d", &N) && N )
{
init();
for(int i = ; i < N; i++)
{
scanf("%d", &sticks[i]);
sum+=sticks[i];
}
sort(sticks, sticks+N, cmp);
for(len = sticks[]; len < sum; len++)
{
if(sum%len==)
{
dfs(, , );
if(flag) break;
}
}
printf("%d\n", len);
}
return ;
}

POJ 1011 Sticks 【DFS 剪枝】的更多相关文章

  1. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  2. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

  3. poj 1011 Sticks ,剪枝神题

    木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿 ...

  4. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  5. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  6. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  7. poj 1011 :Sticks (dfs+剪枝)

    题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...

  8. OpenJudge 2817:木棒 / Poj 1011 Sticks

    1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...

  9. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

    //这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...

随机推荐

  1. 使用 Ninject

    在[ASP.NET MVC 小牛之路]系列上一篇文章(依赖注入(DI)和Ninject)的末尾提到了在ASP.NET MVC中使用Ninject要做的两件事情,续这篇文章之后,本文将用一个实际的示例来 ...

  2. SVN创建资源库和远程连接配置

    SVN创建资源库和远程连接配置 本机安装的是TortoiseSVN-1.7.5.22551-win32-svn-1.7.3.msi 安装好后会在鼠标右键中出现如图最后两项的选项: 创建svn资源库: ...

  3. js中函数带不带var的本质区别是什么

    本质区别是:带var的是定义,属于statement:不带var的是赋值,属于expression.不带var时,解释器认为变量已经定义过了,会在函数中找相应的定义,如果找不到,就会认为变量是在外一层 ...

  4. egret打包android + android微信登录--小结

    公司用egret做了款游戏,需要打android包,做安卓端的微信登录,于是乎开始了第一安卓上的打包,正的是一脸懵 首先遇到的问题有如下: 1. egret打安卓包时经常运行不起来, 主要是gradl ...

  5. 继承Application管理生命周期

    继承Application实现Android数据共享 http://www.jianshu.com/p/75a5c24174b2 jessyan提出一个思路,用Application + 接口来管理扩 ...

  6. 检测IE浏览器兼容Edge模式及IE11

    document.documentMode || +(navigator.userAgent.match(/MSIE (\d+)/) && RegExp.$1) 判断布尔值

  7. 转:ITopologicalOperator Buffer调用异常的解决方法(来源网络)

      /// <summary>   /// 用拓扑分析求出缓冲区范围.   /// 由于ArcGIS的问题,有时调用会出异常,因此需要循环调用   /// </summary> ...

  8. 02_ActiveMQ入门

    [ActiveMQ 入门HelloWorld例子] [启动ActiveMQ] 1.由于本人PC是64位的,选择在bin目录下的win64/activemq.bat启动. 2.启动成功后,访问http: ...

  9. GUID 格式化

    1.Guid.NewGuid().ToString("N") 结果为: 38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid().ToSt ...

  10. JUnit_BeforeClass不报异常的 bug 处理

    1.try{} cathce(Exception e){}将觉得会出问题的地方括起来测试. 2.main方法调用出问题的方法.