题目链接: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. sql常用格式化函数及字符串函数

    一.常用格式化函数 1.日期转字符串 select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') YYYY:年份 MM:月份号(01-12) ...

  2. 用 fmt格式化候时间

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <div ...

  3. 理解 glibc malloc:主流用户态内存分配器实现原理

    https://blog.csdn.net/maokelong95/article/details/51989081 Understanding glibc malloc 修订日志: 2017-03- ...

  4. (转)详解Linux中SSH远程访问控制

    详解Linux中SSH远程访问控制 原文:http://blog.51cto.com/dengqi/1260038 SSH:是一种安全通道协议,主要用来实现字符界面的远程登录,远程复制等功能(使用TC ...

  5. MYSQL系列-MYSQL基础增强(Myql函数)

    MYSQL基础增强(Myql函数) 在这里只介绍一些常用的,比较新颖的: 字符串函数: CONCAT://字符串连接函数 mysql> SELECT CONCAT('My', 'S', 'QL' ...

  6. centOS查看apache版本的命令

    在centOS 7下: 命令如下: httpd -v

  7. Linux安装PHP加速器Xcache

    XCache 是一个又快又稳定的 PHP opcoolcode 缓存器. 经过良好的测试并在大流量/高负载的生产机器上稳定运行. 经过(在linux 上)测试并支持所有现行 PHP 分支的最新发布版本 ...

  8. Javascript模块化编程(一)模块的写法最佳实践六、输入全局变量 独立性是模块的重要特点,模块内部最好不与程序的其他部分直接交互。 为了在模块内部调用全局变量,必须显式地将其他变量输入模块。

    Javascript模块化编程,已经成为一个迫切的需求.理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块但是,Javascript不是一种模块化编程语言,它不支持类clas ...

  9. 上下文(Context)和作用域(Scope)

    函数的每次调用都有与之紧密相关的作用域和上下文.从根本上来说,作用域是基于函数的,而上下文是基于对象的. 换句话说,作用域涉及到所被调用函数中的变量访问,并且不同的调用场景是不一样的.上下文始终是th ...

  10. php服务端学习感想

    php是全世界web开发领域最受欢迎的语言,学习php的人一般都会些前端,懂些html/js/css等,php用得最多的是用于写业务逻辑.如果浮于表面,写过几个月php的人和写几年php的人可能差别不 ...