题目描述:

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. selenium的config.ini

    config.ini # What WebDriver to use for the tests#driver=phantomjs#driver=firefoxdriver=chrome#driver ...

  2. jquery-plugin-biggerLink,highLight-层加亮_andy 阳光生活_百度空间

    How to get the xpath by clicking an html element How to get the xpath by clicking an html element Qu ...

  3. xmu1125 越野车大赛(三分)

    1125: 越野车大赛 Time Limit: 500 MS  Memory Limit: 64 MB  Special JudgeSubmit: 4  Solved: 3[Submit][Statu ...

  4. 教你使用python获得字符串的md5值

    最近需要使用python获取字符串的md5值. 今天把代码贴出来和大家分享一下. 01 #!/usr/bin/env python 02 # -*- coding: cp936 -*- 03 impo ...

  5. (译)Node.js的 EventEmitter 教程

    原文标题:Node.js EventEmitter Tutorial 原文链接:http://www.hacksparrow.com/node-js-eventemitter-tutorial.htm ...

  6. SQLHelper简单版(基础版)

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  7. C++运算符重载为成员函数

    #include<iostream> using namespace std; class Complex{ public: Complex(double r=0.0,double i=0 ...

  8. C程序的存储空间布局

    历史沿袭至今,C程序一直由下列几部分组成: 1. 正文段.这是由CPU执行的机器指令部分.通常,正文段是可共享的,所以即使是频繁执行的程序(编辑器,编译器,命令解释器)在存储器中也只需一个副本,另外正 ...

  9. ural1057 Amount of degrees 位数统计

    #include <iostream> #include <string> using namespace std; ][]; void init(){ f[][] =; ;i ...

  10. c风格字符串函数

    十一.C 风格字符串  1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat(p, p1) 附加字符串  strncat( ...