hdu1455 dfs+剪枝
Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8353 Accepted Submission(s): 2438
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 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.
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
5
#include <iostream>
#include <algorithm>
using namespace std; //total能组成的木棒的组数,l:组成的木棒的长度
int total,l;
//num:输入的整数,sum:总长度
int num,sum;
typedef struct
{
int length;//木棒的长度
bool mark;//木棒是否被使用过
}Sticks;
Sticks sticks[]; bool cmp(Sticks a,Sticks b)
{
return a.length>b.length;
}
//1 0 -1
//s 已组成的木棒数目,len已经组成的长度,pos搜索的木棒的下标的位置
int dfs(int s,int len,int pos)
{
if(s==total)
return ;
for(int i=pos+;i<num;i++){
//如果这个木棒已经用过,则继续下一根
if(sticks[i].mark)
continue;
if(len+sticks[i].length == l)
{
sticks[i].mark = true;
if(dfs(s+,,-))return true;
sticks[i].mark = false;
return false;
}
else if(sticks[i].length+len<l){
sticks[i].mark = true;
if(dfs(s,len+sticks[i].length,i))
return true;
sticks[i].mark = false;
///剪枝:如果当前搜索时,前边的长度为0,而第一根没有成功的使用,
///说明第一根始终要被废弃,所以这种组合必定不会成功
///此处的剪枝必须有,因为这里的剪枝会节省很多的无用搜索,
///我试了一下,此处剪枝省去就会超时的。。。。
if(len==)
return false;
///剪枝:如果当前和上一次搜到的木棒是一样长的则没必要再搜一次了
while(sticks[i].length==sticks[i+].length)
i++;
}
}
return false;
} int main()
{ while(cin>>num&&num!=)
{
sum = ;//标记为0
for(int i = ; i < num; i++)
{
cin>>sticks[i].length;
sum += sticks[i].length;
sticks[i].mark = false;
}
//将木棒按照长度从长到短的顺序排序
sort(sticks,sticks+num,cmp);
//从木棒的最长的那根开始搜索,因为最小的组合也会大于等于最长的那根
for(l = sticks[].length; l <= sum; l++)
{
//剪枝一:如果不能被整除说明不能组成整数根木棒,搜下一个
if(sum%l!=)continue;
total = sum / l;//得到木棒总数目
if(dfs(,,-))
{
cout<<l<<endl;
break;
}
}
}
return ;
}
hdu1455 dfs+剪枝的更多相关文章
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
随机推荐
- 10月21日下午PHP常用函数
函数四要素:返回类型 函数名 参数列表 函数体 //最简单的函数定义方式 function Show() { echo "hello"; } Show();//输出结果为he ...
- C#异常类相关总结
C#异常类相关总结 C#异常类相关总结 C#异常类一.基类ExceptionC#异常类二.常见的异常类1.SystemException类:该类是System命名空间中所有其他异常类的基类.(建议:公 ...
- autofac获取全局Container
_UserService = ((IContainerProviderAccessor)HttpContext.Current.ApplicationInstance).ContainerProvid ...
- Sql2008R2设置远程链接
下边的文章是从百度经验里粘过来的.. 经过测试确实有效..留个备份.. 有个小情况在前边说一下.. 在操作前一定要确定自己的sa用户密码是不是一样..不要以为自己知道的是对的就直接略过某些步骤.. 俗 ...
- yum clean all 是什么意思
yum会将下载下来的 包文件rpm和头文件header存盘在 本地机器的硬盘 缓存中, 这个将占用 硬盘空间, 可以将这些内容清除掉, 以释放磁盘空间: yum clean headers: // 释 ...
- 安装使用ubuntu和opensuse
liquid: n.液体, adj. 液体的, 流动的 liquidate: v. 清洗; 清算; 变现; 杀戮; weird: [wi2d] e:i, ir:2 离奇的,古怪的... 英文名称, 直 ...
- C#对HTML文档的解析
http://www.2cto.com/kf/201312/268777.html http://jingyan.baidu.com/article/7e44095334bb162fc0e2efad. ...
- HTML5语义化标签
在HTML5中最基础也是比较好理解的也就是语义化标签了,,顾名思义语义化也就是可以直接读懂的标签~,这样我们在项目开发过程中不但自己不会因为5花8门的标签命名而伤脑筋,跟同事对接项目也会节约很多时间~ ...
- [译]在AngularJS中何时应该使用Directives,Controllers或者Service
原文: http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/ Services Servic ...
- jquery 获取datagrid行数
var data = $('#dg').datagrid('getData'); alert('总数据量:' + data.total)//注意你的数据源一定要定义了total,要不会为undefin ...