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. grafana快速入门

    入门 本指南将帮助您开始并熟悉Grafana.它假定您有一台正在运行的Grafana服务器,并至少添加了一个数据源. 初学者指南 观看10分钟的初学者指南,以建立仪表板,以快速介绍设置仪表板和面板. ...

  2. HDU5171 矩阵快速幂

    题目描述:http://acm.hdu.edu.cn/showproblem.php?pid=5171 算法: 可以先将数组a[]排序,然后序列 a1 , a2 , … , an 即为有序序列,则第一 ...

  3. 工作流性能优化(敢问activiti有扩展性?)(1)

    工作流待办(首页待办列表),加载缓慢,activiti本机,看了代码又是全部数据加载到内存,然后代码过滤,我为什么又说又呢? 用VisualVM做性能测试:   之前同事给的解决方案: 1.把&quo ...

  4. 64位系统中为VS2012添加OpenGL工具包

    之前一直都是按照网上教程进行的添加,以前使用的系统是32位的,所以一直都没有问题.最近换了64位系统,要使用到OpenGL,于是就又进行了原来的工作,但进行测试时,老是失败: 但是在目录:" ...

  5. IOS 模仿有storyboard的项目控制器的创建

    ● 先加载storyboard文件(Test是storyboard的文件名) UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@ ...

  6. js 实现纯前端将数据导出excel两种方式,亲测有效

    由于项目需要,需要在不调用后台接口的情况下,将json数据导出到excel表格,兼容chrome没问题,其他还没有测试过 通过将json遍历进行字符串拼接,将字符串输出到csv文件,输出的文件不会再是 ...

  7. G++ 编译多个源文件

    g++ -c *.cpp g++ graph.o maxflow.o test.o -o test  // 链接顺序必须写对

  8. 牛客NOIP提高组R1 C保护(主席树)

    题意 题目链接 Sol Orz lyq 我们可以把一支军队(u, v)拆分为两个(u, lca)和(v, lca) 考虑一个点x,什么时候军队对它有贡献,肯定是u或v在他的子树内,且lca在他的子树外 ...

  9. 【计数】cf938E. Max History

    发现有一种奇怪的方法不能快速预处理? 复习一下常见的凑组合数的套路 You are given an array a of length n. We define fa the following w ...

  10. JavaScript(E5,6) 正则学习总结学习,可看可不看!

    1.概述 正则表达式(实例)是一种表达文本模式(即字符串结构)的方法. 创建方式有两种方式: 一种是使用字面量,以斜杠表示开始和结束. var regex = /xyz/ 另一种是使用RegExp构造 ...