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. 宏碁发布两款全A平台笔记本:良心价

    导读 8月3日消息,在全球数码互动娱乐盛会ChinaJoy上,宏碁推出全新两款全A平台笔记本——暗影骑士4 锐龙版酷冷游戏本和蜂鸟Swift3锐龙版金属轻薄本. 此次发布的宏碁暗影骑士4 锐龙版笔记本 ...

  2. Swift 语法糖then

    then是一个swift初始化库,只有80几行的代码库,确可以让初始化变得很优雅. 1.使用then初始化AnyObject,这里以初始化控件为例 lazy var label = UILabel() ...

  3. Arch系统软件列表

    1. 安装统计 2. 安装列表 3. 安装说明 4. 作为依赖项的安装列表 5. 更正 mangaro使用减的方式安装系统.开箱即用的豪华版本,大部分人需要的都有了,同样包括个别用户不需要的,配置方面 ...

  4. 01 vue入门

    vue简介 官网上有介绍,这里粘出来 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心 ...

  5. 8 Jvm堆分析

    备注:直接内存分配,无法触发GC动作 备注:with outgoing reference (当前选中对象引用的对象),with incoming references(引用当前对象的对象)

  6. JS: 复选框——ALL与A、B、C(选中ALL同时选中各子项)

    <!DOCTYPE html><html> <head>  <meta charset="utf-8">  <title> ...

  7. NO3 cat-xargs-cp-mv-rm-find命令

    ·cat            #查看文件内容        eg:cat oldboy.txt·xargs        #从标准输入获取内容创建和执行命令 -n 加数字:分组 ·cp       ...

  8. Window Server 2019 配置篇(6)- 利用组策略实现域内自动安装软件

    上次我们建立了WSUS实现了更新管理,那么现在我们需要的是让集群内的客户机(之后会建立在hyper-v集群上)和服务器都能装上三个软件 1. Microsoft Team 2. Notepad++ 3 ...

  9. Linux每日练习-批量删除用户,非脚本运行方式 20200225

  10. 51nod 1206:Picture 求覆盖周长

    1206 Picture 题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给出平面上的N个矩形(矩形的边平行于X轴和 ...