题目链接: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. web服务器架构演化及所其技术知识体系(分布式的由来)

    文章标题是我自己取的,内容来着百度百科k5665219的一篇回答,觉得讲的很不错就转载过来了. 最开始,由于某些想法,于是在互联网上搭建了一个网站,这个时候甚至有可能主机都是租借的,但由于这篇文章我们 ...

  2. python_面向对象—代码练习

    """注意:代码切勿照搬,错误请留言指出""" import re ''' class Person: name='xxx' age=20 ...

  3. win10 sshsecureshellclient删除profile保存的信息

    C:\Users\joe\AppData\Roaming\SSH

  4. eclipse中注释快捷键

    手动注释: ①类注释:Shift+Alt+J ②方法注释:在方法上方输入/** 后点击回车 自动注释:点击菜单栏上的Window -->Preferences-->Java-->Co ...

  5. Java基础22-Static关键字

    1.static关键字 public class Test{ public static void main(String[] args){ Persion p1=new Persion(); Per ...

  6. PowerDesigner 16.5 安装和卸载教程【含有安装文件】

    1 下载 下载地址:https://pan.baidu.com/s/1kqly0d8qU-QluEagXwh53g 密码:n1a3 下载之后目录结构如下: 2 安装教程 1 安装 1.双击安装文件,如 ...

  7. c#输入方法名来调用方法(反射)

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  8. 最好用的数据存储Easy Save2讲解

    转载:http://www.manew.com/thread-100109-1-1.html   今天抽时间学习了“Easy Save2”插件,版本v2.6.3  我个人觉得这个插件是做数据存取最好的 ...

  9. ife task0003学习笔记(二):JavaScript原型

    function aaa(){} aaa.prototype.bbb=function(){} var obj1=new aaa() var obj2=new aaa() obj1和obj2都有一个属 ...

  10. 【卷土重来之C#学习笔记】(二)c#编程概述

    (1)开始C#,一个简单的程序Hello Word 开始     using System; //使用了System的命名空间 using System.Collections.Generic; us ...