题目描述:

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 Output6

5

代码如下:

 #include<iostream>
#include<cstring>
#include<cstdlib> using namespace std; int stick[],visit[];
int n,len,flag,sum; int cmp(const void * a,const void * b)
{
return (*(int *)a - *(int *)b);
} void dfs(int rest,int complete,int start);
int main()
{
while(cin >> n && n)
{
sum = ;
flag = false;
for(int i = ;i < n;i++)
{
cin >> stick[i];
sum += stick[i];
}
qsort(stick,n,sizeof(int),cmp);//从长到短的排序,先排短木棒的话,很
memset(visit,,sizeof(visit));//有可能出现后面一根木棒都不能填补的情况
for(len = stick[];len <= sum/;len++)//枚举区间应该在sum/2,因为最少
{ //组成两根木棒
if(sum % len == )//拼好的木帮要整除木棒和,这样才能平均分
{
dfs(len,,);
if(flag)//出现符合情况,就退出循环
break;
}
}
if(flag)
cout << len << endl;
else
cout << sum << endl;
}
return ;
} void dfs(int rest,int complete,int start)//rest为拼当前木棒时还缺少的长度
{//complete为已经拼好的木棒数,start为从哪根木棒开始搜索待拼的木棒
if(flag)
return;
if(rest == )
{
complete++;
if(complete == (sum / len))
flag = ;
else
{
int k = ;
while(visit[k])
k++;
visit[k] = ;
dfs(len - stick[k],complete,k+);
visit[k] = ;//回溯
}
}
else
{
for(int i = start;i < n;i++)
{
if(i > && !visit[i - ] && (stick[i - ] == stick[i]))
continue;//如果当前木棒和前一根木棒是一样的长度,而前一根木棒没有用过的话,那么这根木棒也一定不使用的
if(!visit[i] && stick[i] <= rest)
{
visit[i] = ;
dfs(rest - stick[i],complete,i + );
visit[i] = ;
if(rest == stick[i])//注释1 && 注释2(注释1 和 注释2 是同一种解释)
break;
}
}
}
return;
} /*注释1*/
/*搜索过程中,如果这个时候rest==d,也就是说刚刚开始拼一根新的木棒,
* 假如这时把没有用过的最长的一根木棒拼上去,但最后无解的话,
* 那么便不需要去尝试把后面的木棒拼上去了,
* 因为后面不管第几次拼,总要用到这根木棒的,
* 所以出现这种情况,不管后面再尝试拼哪根,最后都一定是无解的。*/ /*注释2*/
/* 搜索过程中,如果遇到一根木棒,它的长度恰好等于rest,
* 但把它拼上去之后无解,那么也便不需要再尝试把后面的木棒拼上去了*/

代码分析:

剪枝非常重要,之前超时了好多次。。。

参考地址:http://www.cnblogs.com/staginner/archive/2011/08/17/2143614.html

Sticks(poj 1011)的更多相关文章

  1. Sticks POJ - 1011 少林神棍 dfs四次剪枝

    http://poj.org/problem?id=1011 题意:若干根棍子被截成小段的木棒,现在给你这些木棒,问最短可以拼出的棍子长度. 题解:搜索,dfs(r,m) 二个参数分别代表还剩r个木棒 ...

  2. DFS(剪枝) POJ 1011 Sticks

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

  3. OpenJudge 2817:木棒 / Poj 1011 Sticks

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

  4. POJ 1011 - Sticks DFS+剪枝

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

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

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

  6. POJ 1011 Sticks 【DFS 剪枝】

    题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

  7. poj 1011 Sticks (DFS+剪枝)

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

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

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

  9. poj 1011 Sticks

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126238   Accepted: 29477 Descrip ...

随机推荐

  1. 探索 Windows Azure 网站中的自动伸缩功能

     去年10月,我们发布了若干针对 WindowsAzure平台的更新,其中一项更新是添加了基于日期的自动伸缩调度支持(在不同的日期设置不同的规则). 在这篇博客文章中,我们将了解自动伸缩的概念,并 ...

  2. HDU 4740 The Donkey of Gui Zhou (模拟)

    由于一开始考虑的很不周到,找到很多bug.....越改越长,不忍直视. 不是写模拟的料...................... 反正撞墙或者碰到已经走过的点就会转向,转向后还碰到这两种情况就会傻站 ...

  3. Linux进程间通信总结

    刚请完婚假,请假期间做了些技术总结,其中一个就是Linux进程间通信方式的总结. Linux提供了多种进程间通信的方式,列举如下: PIPE(管道) FIFO(先进先出,也称为有名管道) domain ...

  4. PS学习之图像选区

    一. 选区的基本操作 快速选择选区与反选选区.取消选区 选择-->全选 或者 CTRL + A  反选CTRL + SHIFT + I ,取消选区 CTRL + D, SHIFT 执行等比例操作 ...

  5. 用 rsync 同步本地和服务器的文件

    参考 DigitalOcean 安装 For Debian/Ubuntu: sudo apt-get install rsync For OpenSUSE/Fedora: sudo yum insta ...

  6. [Swust OJ 632]--集合运算(set容器)

    题目链接:http://acm.swust.edu.cn/problem/632/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  7. C++对象模型3--无重写的单继承

    C++对象模型中加入单继承 不管是单继承.多继承,还是虚继承,如果基于“简单对象模型”,每一个基类都可以被派生类中的一个slot指出,该slot内包含基类对象的地址.这个机制的主要缺点是,因为间接性而 ...

  8. PrintWriter与outputStream区别

    网上截取: printWriter:我们一般用来传的是对像 而outputStream用来传的是二进制,故上传文件时,一定要使用此. PrintWriter以字符为单位,支持汉字,OutputStre ...

  9. Oracle 奇葩的问题:创建存储过程没有反应

    问题描述:需要在oracle 数据库中再创建一个数据库(数据库实例)然后作为临时数据库,一切成功: 现在需要在数据库中新建一个表空间然后创建用户,使用创建的用户登录创建一个存储过程,执行提交刷新一下, ...

  10. discuz默认模板文件结构详解-模板文件夹介绍

    | — template — default   系统内置风格模板(默认风格)| — template — default  – discuz_style_default.xml  风格安装文件,可用 ...