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. 冰蝎动态二进制加密WebShell特征分析

    概述 冰蝎一款新型加密网站管理客户端,在实际的渗透测试过程中有非常不错的效果,能绕过目前市场上的大部分WAF.探针设备.本文将通过在虚拟环境中使用冰蝎,通过wireshark抓取冰蝎通信流量,结合平时 ...

  2. Verilog 2001 `default_nettype none

    在Verilog 1995規定,對於沒宣告的信號會自動視為wire,這樣常常造成debug的困難,Verilog 2001另外定義了`default_nettype none,將不再自動產生wire. ...

  3. 010.CI4框架CodeIgniter, autoload自动加载自己的helper函数类

    01.自己定义了一个helper类,里面有个函数用来输出 02.定义一个Controller基本类,我们以后用到的Controllers类都继承自这个类.其中自动加载helper函数如图所示: 03. ...

  4. eclipse、idea中自动生成元模型JPA元模型对象

    一.eclipse 1.首先准备好两个jar包hibernate-jpa-2.0-api-1.0.1.Final和hibernate-jpamodelgen-4.3.5.Final 2.选中项目右击 ...

  5. mysql 结果排序入门

  6. [题解] UVA11426 GCD - Extreme (II)

    题面 莫反是不可能莫反的,这辈子都不可能莫反了 题目要求的是 \[ \sum\limits_{i=1}^n \sum\limits_{j=i+1}^n \gcd(i,j) \] 稍微变个亚子 \[ \ ...

  7. 九、响应式发:rem和less(适配移动端)

    一.响应式开发 响应式开发优先适配移动端又兼容到pc端 官网:https://less.bootcss.com/usage/ 教程:https://www.w3cschool.cn/less/ rem ...

  8. HttpClient测试

    导入maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson&l ...

  9. python三大神器===》生成器

    1. 认识生成器 利用迭代器,我们可以在每次迭代获取数据(通过next()方法)时按照特定的规律进行生成.但是我们在实现一个迭代器时,关于当前迭代到的状态需要我们自己记录,进而才能根据当前状态生成下一 ...

  10. nodejs学习笔记(一):centos7安装node环境

    由于windows环境安装nodejs只需要访问官方网站下载压缩包,解压即可. 首先检查自己是否安装==wget==,已安装可以跳过这步,未安装则需要先安装: linux yum install -y ...