Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10593    Accepted Submission(s): 3194

Problem 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 file 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
 
题意:原本有一些等长的棍子,棍子数量未知。这些棍子被切成任意段,每段长度50以内。输入被切后棍子的数量和长度,要求出的是未被切时的棍子长度。
 
思路:这题一开始准备用暴力做的,但想着想着感觉跟深搜类似,就尝试用深搜来写了。题目给的数据一定是会使所有木棍凑的出来原来的木棍的,所以就将原来的木棍长度从1开始到所有木棍长度和开始试。将所有木棍从大到小排序,从较长木棍开始选取,到了预定长度就继续选下一个组合成预定长度的木棍组合,如果最后能够将所有木棍成功地组合成预定的长度,这个长度就是最小长度。
重点:剪枝(不进行剪枝的话绝对会超时,剪枝不完全也会超时,亲身经历/(ㄒoㄒ)/~~)
 
 #include<bits/stdc++.h>
using namespace std;
int n,sum_len,g_num,g_len;//所有木棍总长度,目标木棍数量,目标木棍长度
int glen[],vis[];
bool cmp(int a,int b)
{
return a>b;
}
int dfs(int cnt,int len,int pos)
{
if(cnt == g_num) return true;//木棍数量已经搜索到目标木棍数量
if(len == g_len) return dfs(cnt+,,);//木棍长度等于目标木棍长度,目标木棍数量增加1,开始找下一木棒组合成目标木棒长度
for(int i=pos;i<n;i++)//从已经搜索到的木棒向后挑,向前挑长度大于已搜索的,直接不用向前
{
if(!vis[i]&&len+glen[i]<=g_len)//第i根木棒可以增加且没有被选
{
vis[i]=;
if(dfs(cnt,len+glen[i],i+)) return true;//增加后继续搜索
vis[i]=;
if(len==) return false;//剪枝!!!这步省去大量时间,将长度小于最大木棍长度的情况直接舍去
while(i+<n&&glen[i+]==glen[i]) i++;//后面跟前面长度一样,且前面失败了,直接跳
}
}
return false;
}
int main()
{
while(cin>>n)
{
if(n==)
break;
sum_len=;
int glong=;
for(int i=;i<n;i++)
{
cin>>glen[i];
sum_len+=glen[i];
}
for(int i=;i<=sum_len;i++)
{
if(sum_len%i==)
{
memset(vis,,sizeof(vis));
g_len=i;g_num=sum_len/i;
if(dfs(,,))
{
glong=g_len;
break;
}
}
}
cout<<glong<<endl;
}
return ;
}

这个dfs感觉思路跟暴力差不多,如果各位大大能够有更好点的方法,请各位大大不吝赐教O(∩_∩)O!

 

看似不是dfs的dfs HDU-1455的更多相关文章

  1. hdu 1455 Sticks

    Sticks Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  2. hdu 1455 Sticks(dfs+剪枝)

    题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...

  3. hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)

    N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的 Sample Input95 2 1 5 2 1 5 2 141 2 3 40 Sample Output65 ...

  4. hdu 1455(DFS+好题+经典)

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. (DFS)展开字符串 -- hdu -- 1274

    http://acm.hdu.edu.cn/showproblem.php?pid=1274 展开字符串 Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  6. DFS练习一---HDU 1342

    参考文章来源:http://blog.csdn.net/pengwill97/article/details/54850852 题目在这里:HDU.1342 最近在练习DFS,就找了一些题来做,力求自 ...

  7. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  8. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

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

  9. HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...

随机推荐

  1. pat甲级1044二分查找

    1044 Shopping in Mars(25 分) Shopping in Mars is quite a different experience. The Mars people pay by ...

  2. help.hybris.com和help.sap.com网站的搜索实现

    help.hybris.com 我使用help.hybris.com时,发现每次在搜索栏输入文字时,没有发出任何HTTP请求,那么这个自动完成的下拉框里的记录从哪里来的?我看了下实现,发现所有自动完成 ...

  3. RAC基本使用

    @interface ViewController () @property (weak, nonatomic) IBOutlet lwRedView *redView; @property (wea ...

  4. iOS 制作表格 (数据源控制行,列数)

    记得去年面试的过程中,有一个面试官问我怎么制作表格.由于之前也没有做过,当时有点懵逼,今天想起来了,就用tableview制作了一个,望不要有人像我一样掉坑了, 直接上代码: // // ViewCo ...

  5. vi中系统剪切板的设置

    在vi中,如果编译时没有clipboard属性,将vi中的内容拷贝到系统剪切板有些麻烦.可以用如下的方法,查看vi 是否支持系统剪切板的功能: xt@xt-ThinkPad-X220:~$ vi -- ...

  6. flask-bootstrap

    pip install bootstarp 使用bower安装bootstrap的命令是: bash$ bower install bootstrap不过问题出在如何安装bower上. 官方网站上这样 ...

  7. 四、Shell 数组

    Shell 数组 数组中可以存放多个值.Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似). 与大部分编程语言类似,数组元素的下标由0开始. She ...

  8. php短网址生成算法

    <?php //短网址生成算法 class ShortUrl { //字符表 public static $charset = "0123456789ABCDEFGHIJKLMNOPQ ...

  9. vue 组件的书写

    简单的来说是 vue组件最核心的就是props和自定义函数,来实现组件的开发 最简单的一个组件 子组件如下: <template> <div class="bgClass& ...

  10. ZendFramework-2.4 源代码 - 关于MVC - Model层类图