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. Java数组去重的方法

    //第一种方式:最开始想到的是利用Set集合的不可重复性进行元素过滤 public static Object[] oneClear(Object[] arr){  Set set = new Has ...

  2. 5G大潮下卖点越来越少的苹果,会成为下一个诺基亚吗?

    当下,5G显然成为手机厂商继全面屏.AI等之后,又一个重要的风口.为了赶上这个风口,很多厂商不惜寻找各种噱头去吸引大众的关注.比如小米在发布MIX 3之前疯狂造势称要发布5G版,但在10月25日的发布 ...

  3. Windows环境下的32位汇编语言程序设计

    一个逆向的告诉我可以尝试学一下8086处理器,再回头看一看自己学过的会有提高学呗,8086处理器怎么学....然后就学了8086的汇编, 好友就分享了琢石成器——Windows环境下的32汇编语言设计 ...

  4. stm32CubeMx CAN 发送数据

    平台  STM32F429 软件  STM32CubeMx 5.0.0 固件库  STM32Cube_FW_F4_V1.23.0 目的: 实现 CAN 的发送 一  简介 CAN是控制器局域网络(Co ...

  5. tensorflow学习笔记(二)常量、变量、占位符、会话

    常量.变量.占位符.会话是tensorflow编程的基础也是最常用到的东西,tensorflow中定义的变量.常量都是tensor(张量)类型. 常量tf.constant() tensorflow中 ...

  6. leetcode102 Binary Tree Level Order Traversal

    """ Given a binary tree, return the level order traversal of its nodes' values. (ie, ...

  7. 适配器之SimpleAdapter

    前言: 在写适配器时,SimpleAdapter会经常使用到,虽然他比ArrayAdapter复杂,但是也提供了更多的功能 正文: 我们接下来先从SimpleAdapter中较为简单的显示两行文本开始 ...

  8. JavaScript原生Ajax请求纯文本数据

    源代码 ajax1.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...

  9. PAT Advanced 1072 Gas Station (30) [Dijkstra算法]

    题目 A gas station has to be built at such a location that the minimum distance between the station an ...

  10. 打包|zip

    原始:gzip zip -r ./gzip.zip ./gzip/* adding: gzip/split_10.gz (deflated 2%) adding: gzip/split_11.gz ( ...