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

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

Source

题目链接:http://poj.org/problem?id=1011

题目大意:有n根木棍。用它们拼成一些等长的木棍,求拼出的木棍的最短长度。

解题思路:这题的时间限制特别严格。

DFS+剪枝,剪枝较多。首先由多到少枚举木棍数目num。即从n到1,要满足木棍总长度是num的倍数,且拼出的长度要不小于最长的木棍长度,否则无法拼,搜索到答案后退出循环,保证求出的木棍长最短。

剪枝:1.木棍由长到短排序。

2.訪问过的木棍或者加上当前木棍长度后超过了目标长度,则跳过本次循环。

3.若当前木棍和上一根木棍长度同样而且上一根木棍没用到,则跳过本次循环。

4.dfs中标记開始木棍下标。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[66],vis[66];
int n,num,m;
bool p;
int cmp(int a,int b)
{
return a>b;
}
void dfs(int st,int cur,int cnt)
{
if(p||cnt==num)
{
p=true;
return ;
}
for(int i=st;i<n;i++)
{
if(vis[i]||cur+a[i]>m) //訪问过的木棍或者加上当前木棍长度后超过了目标长度,则跳过本次循环
continue;
if(i-1&&!vis[i-1]&&a[i]==a[i-1]) //若当前木棍和上一根木棍长度同样而且上一根木棍没用到,则跳过本次循环。
continue;
if(a[i]+cur==m)
{
vis[i]=1;
dfs(0,0,cnt+1);
vis[i]=0;
return; //循环里后面的值都在dfs中求过了。这里直接返回上一层
}
if(a[i]+cur<m)
{
vis[i]=1;
dfs(i+1,a[i]+cur,cnt);
vis[i]=0;
if(cur==0) //cur为0时,直接返回上一层
return ;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
int sum=0;
p=false;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
sort(a,a+n,cmp);
for(num=n;num>=1;num--)
{
if(sum%num||a[0]>sum/num)
continue;
m=sum/num;
dfs(0,0,0);
if(p)
break;
}
printf("%d\n",m);
}
return 0;
}

poj 1011 Sticks (DFS+剪枝)的更多相关文章

  1. POJ 1011 - Sticks DFS+剪枝

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

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

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

  3. poj 1011 Sticks ,剪枝神题

    木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿 ...

  4. DFS(剪枝) POJ 1011 Sticks

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

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

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

  6. POJ 1011 Sticks 【DFS 剪枝】

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

  7. poj 1011 :Sticks (dfs+剪枝)

    题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...

  8. OpenJudge 2817:木棒 / Poj 1011 Sticks

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

  9. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

    //这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...

随机推荐

  1. <httpProtocol/>配置http协议头

    Web.Config中的位置 <configuration> <system.webServer> <httpProtocol> <!--http协议内容-- ...

  2. 查看 SELinux状态及关闭SELinux

    查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 SELinux status:         ...

  3. 动态更新UI的方式

    1. TimerTask 和 timer连用: 这里主要是实现倒计时, TimerTask 里面有方法runOnUiThread,在这个方法里面调用timer cancel()停止倒计时,同样更新UI ...

  4. mysql与java数据类型对应关系

  5. JQuery实现隔行变色和突出显示当前行 效果

    运行效果如下图: jquery关键代码: <script type="text/javascript"> //该文件为:js.js // 当鼠标移到表格上是,当前一行背 ...

  6. Flask学习记录之Flask-Migrate

    一.配置Flask-Migrate from flask.ext.migrate import Migrate, MigrateCommand migrate = Migrate(app,db) #第 ...

  7. C 语言链表操作例程 (待完善)

    #include<stdio.h>#include<malloc.h>#include<conio.h>#include<stdlib.h>#inclu ...

  8. 部署在腾讯云的公益网站遭受了一次CC攻击

    版权声明:本文由黄希彤  原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/651089001483090830 来源:腾云阁 ...

  9. Lintcode--009(单词切分)

    http://www.lintcode.com/zh-cn/problem/word-break/ 单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. ...

  10. 关于炒股软件——金魔方炒股软件的Dll外挂开发

    2015-01-19 14:40:04 金魔方平台是由飞狐交易师原创团队集多年研发经验,依靠和讯财经网强大资源,吸取国际专家思路而推出的十年巨作.目前新出的这个2.0版,这一版在数据存储方面作很大的改 ...