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. HDU 1028 Ignatius and the Princess III伊格和公主III(AC代码)母函数

    题意: 输入一个数n,求组合成此数字可以有多少种方法,每一方法是不记录排列顺序的.用来组成的数字可以有1.2.3....n.比如n个1组成了n,一个n也组成n.这就算两种.1=1,2=1+1=2,3= ...

  2. mysqlimport命令

    mysqlimport的大多数选项直接对应LOAD DATA INFILE子句. 选项: -u,--user 指定连接用户名.   -p,--password[name] 指定连接用户的密码.   - ...

  3. expect脚本中,变量的写法

    一.expect脚本中,变量的不同写法 shell脚本中定义时间变量的写法:time=`date "+%Y%m%d"` ==>>直接照搬到expect中,设置的变量是不 ...

  4. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第八节

    原文链接 第八节:利用CUDA函数库 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的实验室进 ...

  5. 第四篇、Swift_Podfile文件配置格式

    # Uncomment this line to define a global platform for your project platform :ios, '9.0' # Comment th ...

  6. 博学谷-数据分析pandas

    import pandas as pd df=pd.read_csv() df=pd.read_sql()

  7. Vue3.0脚手架搭建

    https://www.jianshu.com/p/fbcad30031c2 vue3.0官网:https://cli.vuejs.org/zh/guide/ 介绍: notice: 这份文档是对应 ...

  8. SpringMVC URL模板模式映射

    使用@RequestMaping和@PathVariable 组合使用 通过 @PathVariable  可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可 ...

  9. Linux常用关机重启命令

    # shutdown -h 10       //计算机将在10分钟后 关机,且会显示在登录用户的当前屏幕中 # shutdown -h now    //立即 关机 # shutdown -h 20 ...

  10. windows 解决缺失.dll的问题

    1.缺失MSVCR120.dell和MSVCP120.dll,如图: 这种问题是因为没有Microsoft Visual C++ 2013运行库的问题,自行百度在Microsoft官网下载即可,注意需 ...