题目描述:

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. Azure 网站和通配符域

     本文章由Azure 网站团队软件开发工程师Michael Candido 撰写 一些 Web 应用程序需要使用多个子域,在某些情况下还需要动态添加新的子域.例如,一个多租户 Web 应用程序可使 ...

  2. 蓝桥杯之FBI树问题

    问题描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含"0&q ...

  3. Ping pong(树状数组经典)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. unity3d插件Daikon Forge GUI 中文教程-5-高级控件listbox和progress bar的使用

    (游戏蛮牛首发)大家好我是孙广东.官网提供了专业的视频教程http://www.daikonforge.com/dfgui/tutorials/,只是是在youtube上,要观看是须要FQ的. 只是教 ...

  5. 关于javascript的沙箱模式以及缓存方法

    在javascript函数代码中,经常会不经意出现全局变量,很可能造成对全局对象的污染,由于这种弊端的存在,那么沙箱模式油然而生.沙箱模式又称为沙盒模式.隔离模式.在js中只有函数可以限定变量作用域, ...

  6. 【踩坑】近来在Firefox上遇到的一些坑

    因为工作一年多以来,做的工作基本都是和webkit系列打交道. 先是做m站,后来做了两个app内嵌的hybrid项目,从来只考虑webkit前缀和相关的伪类. 最近四个多月开始做内部的管理系统,写写样 ...

  7. connot find one or more components. please reinstall the application

    正在用 Visual Studio 2013 写程序,程序一直执行正常. 此时,手动把注册表"HKEY_USERS"的当前用户的权限删除.再运行程序会提示:“是否继续并运行上次的成 ...

  8. UVa202 Repeating Decimals

    #include <stdio.h>#include <map>using namespace std; int main(){    int a, b, c, q, r, p ...

  9. Codeforces 116C - Party(dfs)

    n个人,每个人之多有一个上司.“上司”关系具有传递性.求最少将人分成多少组,每组中的每个人的上司或者间接上司都不在该组.拿到题就用树的直径wa了一炮... 正解是有向无环森林的最长路.从每个跟节点df ...

  10. C——货物管理系统

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> ...