Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 128734   Accepted: 30173

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根木棒,这些木棒是由若干个长度相同的木棒cut来的,原来的木棒长度不知道了,原来有多少根不知道了,需要你去求最小的可能的木棒长度。

这题是POJ2362的加强版,从棒子的最大长度开始搜起,一直到sum。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int num_s,sum,temp;
int stick[70];
bool visit[70]; bool dfs(int num,int length,int stick_st,int * stick,bool *visit)
{
if(num==(sum/temp))
return true;
int i;
int sample=-1;
for(i=stick_st;i<=num_s;i++)
{
if(visit[i]||stick[i]==sample)continue;//剪枝3,之前不行的木棒,再次碰到相同长度的,直接cut。 visit[i]=true;
if(length+stick[i]<temp)
{
if(dfs(num,length+stick[i],i+1,stick,visit))
{
return true;
}
else
{
sample=stick[i];
}
}
else if(length+stick[i]==temp)
{
if(dfs(num+1,0,1,stick,visit))
{
return true;
}
else
{
sample=stick[i];
}
}
visit[i]=false; if(length==0)//剪枝4,给你最好的条件你都不作为,那么接下来这跟木棒是不可能有任何作为的,cut。
break;
}
return false;
} bool cmp(const int a,const int b)
{
return a>b;
} int main()
{
int i;
while(1)
{
scanf("%d",&num_s);
if(num_s==0)
break; sum=0;
for(i=1;i<=num_s;i++)
{
scanf("%d",&stick[i]);
visit[i]=false;
sum += stick[i];
}
sort(stick+1,stick+1+num_s,cmp); bool flag=false;
for(temp=stick[1];temp<=sum/2;temp++)//剪枝1,最多就实验到sum/2,如果sum/2都不行的话,就说明只能是sum了。因为要把木棍分成几个啊,都大于sum的一半了肯定分不了了啊
{
if(sum%temp==0)//剪枝2
{
if(dfs(1,0,1,stick,visit))//第一个数代表要当前完成第几根木棒
//第二个数代表当前已经完成的长度
//第三个数代表木棒从第几个开始找起的
{
cout<<temp<<endl;
flag=true;
break;
}
}
}
if(!flag)
cout<<sum<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1011:Sticks 经典搜索的更多相关文章

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

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

  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 搜索

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

  8. poj(1011)——Sticks(经典的dfs+剪枝)

    题目的大致意思是: 如今有n根木棍,然后须要把它们拼成相同长度的木棍,问满足这个条件的最短的长度是多少? 想法嘛:那肯定是dfs把长度搜一遍就好,但问题的关键是这里会超时.那么就要用到剪枝的原理了. ...

  9. poj 1011 Sticks (DFS+剪枝)

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

随机推荐

  1. eos 智能合约开发体验

    eos编译安装 eos 特性 数据存储 eos投票智能合约开发 eos投票智能合约部署测试 注意避坑 eos编译安装 ERROR: Could not find a package configura ...

  2. dubbo-admin的安装使用

    dubbo-admin下载地址:https://github.com/apache/incubator-dubbo/releases 可以直接下载到linux上 wget https://github ...

  3. 基于RabbitMQ的MQTT协议及应用

    MQTT的开源代码地址先贴在这里:https://github.com/mqtt/mqtt.github.io/wiki/servers MQTT定义: MQTT(Message Queuing Te ...

  4. css - flex 定义排列方向

    flex-direction定义伸缩项目放置在伸缩容器的排列方向,对应有四个值: (1)row:从左到右或从右到左 (2)row-reverse:与row属性相反 (3)column:从上到下排列 ( ...

  5. 《Interest Rate Risk Modeling》阅读笔记——第十章 主成分模型与 VaR 分析

    目录 第十章:主成分模型与 VaR 分析 思维导图 一些想法 推导 PCD.PCC 和 KRD.KRC 的关系 PCD 和 KRD PCC 和 KRC 第十章:主成分模型与 VaR 分析 思维导图 一 ...

  6. Spark Scheduler 模块(下)

    Scheduler 模块中最重要的两个类是 DAGScheduler 和 TaskScheduler.上篇讲了 DAGScheduler,这篇讲 TaskScheduler. TaskSchedule ...

  7. Consul 简介及集群安装

    简介 Consul是基于GO语言开发的开源工具,主要面向分布式,服务化的系统提供服务注册.服务发现和配置管理的功能. Consul的功能都很实用,其中包括:服务注册/发现.健康检查.Key/Value ...

  8. Oracle 建库

    Oracle得安装就不多说了 不过还是建议直接去官网下  其他渠道可能会导致安装问题  具体自己慢慢体会吧  !  下面主要说下怎么用Oracle建库并且建用户角色 Database configur ...

  9. 用四种方法将两个AJAX改为同步

    用四种方法将两个AJAX改为同步 Promise.Generator函数.yield.async/await 相关 今有一题,题目为: 现有ajax1()和ajax2(),用于快速初始化CODE1和C ...

  10. 104-PHP定义并实例化类

    <?php class ren{ //定义人类 } class mao{ //定义猫类 } echo '实例化一个人类:'; var_dump(new ren()); //实例化人类 echo ...