题目链接: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. ELK 插件(一) ---- head

    一, 插件介绍 01, ElasticSearch Head是什么? ElasticSearch Head是集群管理.数据可视化.增删查改.查询语句可视化工具.可以对集群进行傻瓜式操作.你可以通过插件 ...

  2. C# MODBUS 通信

    背景 电厂有多组监控设备,需要在指定的设备上显示某些数据(其他设备对接过来的).通信协议是modbus主从结构. 源码: http://download.csdn.net/download/wolf1 ...

  3. 【Tensorflow】 Object_detection之配置Training Pipeline

    参考:Configuring an object detection pipeline 1.config文件 配置好的config文件存放路径:object_detection/samples/con ...

  4. (转)Linux网络状态工具ss命令使用详解

    Linux网络状态工具ss命令使用详解 原文:http://www.landui.com/help/show-5991.html ss 是 socket statistics 的缩写.顾名思义,ss ...

  5. 再探canvas(小球实例)

    之前学习过canvas的一些使用,也用过canvas绘制过时钟, 但是很久不用,有些遗忘了,这里做一个简单的回顾. 在web页面创建一个canvas画布非常简单,如下即可: <canvas id ...

  6. 案例40-层与层之间的解耦(面向接口编程)BeanFactory

    1 bean.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans> <!- ...

  7. tabs(标签页的现成页面)原生js写法

    直接上代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  8. 网站加入QQ聊天链接

    有时候我们的网站需要加入客服聊天功能,实现方式各不相同同,对于流量不大的网站,可以加入qq聊天的链接,点击链接,会打开本地qq的聊天窗口, 和指定的人会话.实现方式很简单,就是一个<a>标 ...

  9. HDU 5635 ——LCP Array ——————【想法题】

    LCP Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  10. 转帖:kindeditor编辑区空格被隐藏,导致所见所得不一致的解决办法

    1.修改kindereditor-all.js中的 var re = /(\s)<(/)?([\w-:]+)((?:\s+|(?:\s+[\w-:]+)|(?:\s+[\w-:]+=[^\s&q ...