Sticks

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

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
 
Source
#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+剪枝的更多相关文章

  1. *HDU1455 DFS剪枝

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

  2. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  3. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  4. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  5. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  6. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

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

  8. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

  9. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

随机推荐

  1. 9月5日网页基础知识 通用标签、属性(body属性、路径、格式控制) 通用标签(有序列表、无序列表、常用标签)(补)

    网页基础知识 一.HTML语言 HTML语言翻译汉语为超文本标记语言. 二.网页的分类 1.静态页面:在静态页面中修改网页内容实际上就是修改网页原代码,不能从后台操作,数据来只能来源于原于代码.静态网 ...

  2. memcached命令行操作详解,命令选项的详细解释

    连接到memcached命令行下:  telnet 127.0.0.1 11211 1.set / add / replace : 格式:<command> <key> < ...

  3. js的DOM对象

    1.js的Array对象           ** 创建数组(三种)                          - var arr1 = [1,2,3];                    ...

  4. 两种html幻灯片效果

    650) this.width=650;" src="http://img1.51cto.com/attachment/201307/165757318.jpg" tit ...

  5. 3步完成chrome切换搜索引擎

    1.打开chrome://settings/,找到搜索 2.点击“管理搜索引擎”,出现弹窗. 增加搜索引擎,三个文本框分别输入:名称.快捷键.地址 3.在新的选项卡中,输入快捷键(如:github), ...

  6. iOS- storyboard this class is not key value coding-compliant for the key xxx

    如图: 在使用storyboard的时候出现此问题,主要是因为给storybroad中的view拖线的时候,有时不小心线拖错了,或者再次拖线导致代码中控件的名字与之前拖线时定义的名字不同导致的. 解决 ...

  7. shell脚本笔记(原创不断记录)

    今天开始自己的shell脚本练习,刚好公司有太服务器,要时间对数据的cp是按月的: 考虑:首先寻找规律,发现都放置在/opt/www/aaa/  里面有很多的2级和3级目录和文件,但我追踪要备份的是年 ...

  8. 避免重定向301&302 (Avoid Redirects)

    这个也是Best Practices for Speeding Up Your Web Site的第12条原则: 重定向的意思是,用户的原始请求(例如请求A)被重定向到其他的请求(例如请求B).这是H ...

  9. 关于360的META设置,强制使用极速模式

    我的网站,为了使360浏览器打开时默认为极速模式,给用户良好的体验!避免网页由于细节而导致页面布局错乱~ <!DOCTYPE HTML> <html> <head> ...

  10. 工具介绍 - VSCommands

    VSCommands 一个Visual Studio的轻量级扩展工具 地址:http://vscommands.squaredinfinity.com/home 1.可以设置自动隐藏显示主菜单栏,设置 ...