题目链接

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 题意:有n段木棍,要求将这n根木棍拼成x跟长度相同的木棍,要使新的木棍尽量短,输出最小值? 思路:搜索,新的木棍的长度一定大于等于原来木棍的最大值maxlen,小于等于这些木棍的总长度sum,所以从小到大(maxlen~sum)遍历这些值,如果sum%i!=0 ,则肯定不能拼成合法的木棍直接跳过;
如果sum%i==0,那么有可能满足,则进行搜索。搜索过程:一根一根的深搜,记录当前已经拼成几根木棍,当前拼的这跟木棍还差多长,用过的木棍用v[i]进行标记。 代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int start,sum;
int v[]; int check(int num,int rest,int pos)
{
if(num==sum/start) return ;
rest-=pos; v[pos]--;
if(rest==)
{
int i;
for(i=; i>=; i--) if(v[i]) break;
int flag=check(num+,start,i);
v[pos]++;
return flag;
}
for(int i=rest; i>=; i--)
{
if(!v[i]) continue;
int flag=check(num,rest,i);
if(flag) return ;
}
v[pos]++;
return ;
} int main()
{
int n;
while(scanf("%d",&n)&&n)
{
int maxn=-;
sum=;
memset(v,,sizeof(v));
for(int i=; i<=n; i++)
{
int x; scanf("%d",&x);
sum+=x;
v[x]++;
maxn=max(maxn,x);
}
for(start=maxn; start<=sum; start++)
{
if(sum%start!=) continue;
if(check(,start,maxn))
{
printf("%d\n",start);
break;
}
}
}
return ;
}
/**
9
21 16 33 36 19 1 35 6 47
ans=107 43
46 16 47 31 22 48 10 47 25 48 33 31 35 33 14 21 8 22 20 37 20 48 8 18 3 44 28 16 9 50 44 18 46 28 43 49 18 19 31 46 3 43 43
ans=141
*/ /**
20
4 2 1 6 6 5 6 9 1 0 6 9 0 4 8 3 2 1 1 6
20
6 8 9 4 5 10 6 5 8 5 5 7 9 6 3 10 3 1 9 9
*/

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

  1. POJ 1011 sticks 搜索

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 125918   Accepted: 29372 Descrip ...

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

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

  3. DFS(剪枝) POJ 1011 Sticks

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

  4. POJ 1011 - Sticks DFS+剪枝

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

  5. OpenJudge 2817:木棒 / Poj 1011 Sticks

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

  6. POJ 1011 Sticks 【DFS 剪枝】

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

  7. POJ 1011 Sticks(搜索 && 剪枝 && 经典)

    题意 : 有n根木棍(n<=64),它们由一些相同长度的木棍切割而来,给定这n根木棍的长度,求使得原来长度可能的最小值. 分析 : 很经典的深搜题目,我们发现答案只可能是所有木棍长度总和的因数, ...

  8. poj 1011 Sticks (DFS+剪枝)

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

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

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

  10. POJ 1011 Sticks(dfs+剪枝)

    http://poj.org/problem?id=1011 题意:若干个相同长度的棍子被剪成若干长度的小棍,求每根棍子原来的可能最小长度. 思路:很经典的搜索题. 我一开始各种超时,这题需要很多剪枝 ...

随机推荐

  1. 前端数据存储方案集合(cookie localStorage等)以及详解 (二)

    前端数据存储方案集合(cookie localStorage等)以及详解 (二) 在之前的文章中已经介绍到了 前端存储方案中的 cookie . 但是 cookie 的存储上限是 4KB. 如果超过了 ...

  2. [ZYNQ-7]PS处理PL外部中断的简单实例的剖析 (参考米联miz702n)

    Zynq的ARM通过GIC中断控制器来接收核仲裁所有的中断.由于中断向量表只有4Bytes大小,仅仅正好存放一条跳转语句,因此当产生一个外部中断时,中断处理的大致过程:PC内容保存到LR_IRQ用于中 ...

  3. Oracle维护:每天的工作

    Oracle维护:每天的工作 检查数据库状态 确认所有的INSTANCE状态以及listener状态正常,登陆到所有数据库或例程,检测ORACLE后台进程: $ ps –ef|grep ora $ l ...

  4. MATLAB中绘制图形的时候,坐标和标题倒置

    1.如上图所示,直方图的坐标轴以及标题文字都颠倒了 原因: 在MATLAB显示的subplot函数中,图像与直方图这些不属于一类,所以在显示的时候会出现这种情况 解决办法:1>将图像与直方图分开 ...

  5. RobotFramework自动化测试框架-移动手机自动化测试AppiumLibrary库其它的常见自动化关键字

    关键字 使用描述 Close Application 关闭掉当前已经打开的APP Application,该关键字不需要接收任何的参数,但是使用该关键字的前提是已经打开了一个APP Applicati ...

  6. logstash结合zabbix报警安装部署

    cd /usr/share/logstash/ vim Gemfile source "https://ruby.taobao.org/" ##修改成国内镜像站 source &q ...

  7. Java 中判断 JSONObject 对应的 VALUE 为空

    目前发现有两种包.两种不一样的json包. 第一种情况是: json包是json-lib包是net.sf.json 怎样判断JSONObject返回的是字符串null还是null值. 研究源码发现.J ...

  8. OpenPop.NET

    OpenPop用于接收邮件,可以读取邮件内容或者邮件附件内容导入到系统中 http://hpop.sourceforge.net/documentation/index.html

  9. 【Ubuntu16]】ufw

    Usage: ufw COMMAND Commands: enable enables the firewall 开启ufw防火墙 disable disables the firewall 禁用防火 ...

  10. 《principles of model checking》中的离散时间马尔科夫链

    <principles of model checking>中的离散时间马尔科夫链 说明:此文为我自学<principles of model checking>第十章内容的笔 ...