题目描述:

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 Output6

5

代码如下:

 #include<iostream>
#include<cstring>
#include<cstdlib> using namespace std; int stick[],visit[];
int n,len,flag,sum; int cmp(const void * a,const void * b)
{
return (*(int *)a - *(int *)b);
} void dfs(int rest,int complete,int start);
int main()
{
while(cin >> n && n)
{
sum = ;
flag = false;
for(int i = ;i < n;i++)
{
cin >> stick[i];
sum += stick[i];
}
qsort(stick,n,sizeof(int),cmp);//从长到短的排序,先排短木棒的话,很
memset(visit,,sizeof(visit));//有可能出现后面一根木棒都不能填补的情况
for(len = stick[];len <= sum/;len++)//枚举区间应该在sum/2,因为最少
{ //组成两根木棒
if(sum % len == )//拼好的木帮要整除木棒和,这样才能平均分
{
dfs(len,,);
if(flag)//出现符合情况,就退出循环
break;
}
}
if(flag)
cout << len << endl;
else
cout << sum << endl;
}
return ;
} void dfs(int rest,int complete,int start)//rest为拼当前木棒时还缺少的长度
{//complete为已经拼好的木棒数,start为从哪根木棒开始搜索待拼的木棒
if(flag)
return;
if(rest == )
{
complete++;
if(complete == (sum / len))
flag = ;
else
{
int k = ;
while(visit[k])
k++;
visit[k] = ;
dfs(len - stick[k],complete,k+);
visit[k] = ;//回溯
}
}
else
{
for(int i = start;i < n;i++)
{
if(i > && !visit[i - ] && (stick[i - ] == stick[i]))
continue;//如果当前木棒和前一根木棒是一样的长度,而前一根木棒没有用过的话,那么这根木棒也一定不使用的
if(!visit[i] && stick[i] <= rest)
{
visit[i] = ;
dfs(rest - stick[i],complete,i + );
visit[i] = ;
if(rest == stick[i])//注释1 && 注释2(注释1 和 注释2 是同一种解释)
break;
}
}
}
return;
} /*注释1*/
/*搜索过程中,如果这个时候rest==d,也就是说刚刚开始拼一根新的木棒,
* 假如这时把没有用过的最长的一根木棒拼上去,但最后无解的话,
* 那么便不需要去尝试把后面的木棒拼上去了,
* 因为后面不管第几次拼,总要用到这根木棒的,
* 所以出现这种情况,不管后面再尝试拼哪根,最后都一定是无解的。*/ /*注释2*/
/* 搜索过程中,如果遇到一根木棒,它的长度恰好等于rest,
* 但把它拼上去之后无解,那么也便不需要再尝试把后面的木棒拼上去了*/

代码分析:

剪枝非常重要,之前超时了好多次。。。

参考地址:http://www.cnblogs.com/staginner/archive/2011/08/17/2143614.html

Sticks(poj 1011)的更多相关文章

  1. Sticks POJ - 1011 少林神棍 dfs四次剪枝

    http://poj.org/problem?id=1011 题意:若干根棍子被截成小段的木棒,现在给你这些木棒,问最短可以拼出的棍子长度. 题解:搜索,dfs(r,m) 二个参数分别代表还剩r个木棒 ...

  2. DFS(剪枝) POJ 1011 Sticks

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

  3. OpenJudge 2817:木棒 / Poj 1011 Sticks

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

  4. POJ 1011 - Sticks DFS+剪枝

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

  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+剪枝)

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

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

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

  9. poj 1011 Sticks

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126238   Accepted: 29477 Descrip ...

随机推荐

  1. oracle dataguard 角色切换

  2. display:table标签来自动改变列宽 改变的同时table的整体宽度跟随变化

    发现公司里的所有分页功能都是通过display:talbe来实现的,但是用户最近说要让表格列宽可以拖动:所有我就寻找了好多的办法:网上找了很多的资料,但是都不是我要的效果因为他们都是列宽不改变要不就是 ...

  3. 从头開始写项目Makefile(七):统一目标输出文件夹

    [版权声明:转载请保留出处:blog.csdn.net/gentleliu. Mail:shallnew at 163 dot com]     上一节我们把规则单独提取出来,方便了Makefile的 ...

  4. javascript中对变量类型的推断

    本文正式地址:http://www.xiabingbao.com/javascript/2015/07/04/javascript-type 在JavaScript中,有5种基本数据类型和1种复杂数据 ...

  5. 【c语言】推断一个数是不是2的n次方

    // 推断一个数是不是2的n次方 #include <stdio.h> void judge_n(int a) { int b = a - 1; if ((a & b) == 0) ...

  6. jQuery validate api(转)

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  7. 建造者模式->代码示例

    <?php interface Builder{ public function head(); public function body(); public function foot(); ...

  8. 原生js实现的轮播图,易用+可多用

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

  9. poj 3764 The xor-longest Path Trie

    题目链接 求树上的一条最长异或路径. 定义f(u, v)为u到v的路径, 那么显然f(1, u)^f(1, v) = f(u, v), 想不到这个就没有办法做. 然后就可以用字典树查询+插入了. 用指 ...

  10. MVC-04 视图(2)

    三.View如何从Aciton取得数据 从Action取得数据,在ASP.NET MVC可区分成两种方式,一种是“使用弱类型取得数据”,另一种则是“使用强类型取得数据”,两者的差别在于View页面最上 ...