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. window成员和document成员

    输出浏览器成员和DOM成员(以下为safari浏览器测试)(浏览器不同对象成员有差异) window成员 <script type="text/javascript"> ...

  2. CSS选择器列表

    h1 类型选择器 选择元素的一个类型 .className 类选择器 以class属性的值来选择元素,可以在一个页面中出现多个 #idName ID选择器 以id属性的值来选择元素,在页面中是唯一的, ...

  3. C库专题(Day1)

    <assert.h> C库宏-assert()   定义:#define assert(ignore) ((void)0) void assert(int experession); ex ...

  4. Collection使用方法

    package cn.stat.p3.conection.demo; import java.util.ArrayList; import java.util.Collection; import j ...

  5. 左右推拽显示对比图 - jQyery封装 - 附源文件

    闲来无事,做了一个模块效果 左右拖拽显示对比图,是用jq封装的 利用鼠标距离左侧(0,0)坐标的横坐标位移来控制绝对定位的left值 再配合背景图fixed属性,来制作视觉差效果 代码如下 <! ...

  6. QQ联系客服

    $('.side').bind('mouseenter mouseleave',function(e){ if(e.type=="mouseenter"){ if(!$(this) ...

  7. C 小写字母编程大写并输出

    main(){ FILE *fp; char str[100],filename[10]; int i=0;if((fp=fopen("test","w"))= ...

  8. AFNetworking 使用总结 (用法+JSON解析)

    « AFNetworking 图片的本地缓存问题 Get application bundle seed ID in iOS » AFNetworking 使用总结 (用法+JSON解析)    Fr ...

  9. DOCKER功能练习

    都是书上的示例,慢慢进入..

  10. Spring Boot使用redis做数据缓存

    1 添加redis支持 在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> & ...