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. 初识Python类

    吐槽:学习面向对象就像你追一个女神一样,刚刚有点感觉了,过几天又陷入绝望的感觉,很蛋疼. 类的语法 class Person(object): print("learning class&q ...

  2. ecshop 快速添加会员

    /*------------------------------------------------------ */ //-- 快速添加会员 /*-------------------------- ...

  3. Django笔记-登陆、注册(利用cookie实现)

    1.项目结构: 2.关键代码: settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'djang ...

  4. 单元测试写cookie

    我们在开发WEB项目的时候,一般应用逻辑跟ASPX页面是分离的项目.应用逻辑一般会是一个DLL组件项目.如果这个组件项目中A方法使用了Session.Cookie等信息的读写,则这个方法就很难写单元测 ...

  5. MyEclipse 中各种 libraries 的含义

    MyEclipse 中各种 libraries 的含义       JRE System Library,Java EE 5 Libraries,Referenced  Libraries这三个都是各 ...

  6. python模块time&datetime&json & picle&14.logging等

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  7. Media Wiki

    https://www.mediawiki.org/wiki/Help:Images/zh https://www.mediawiki.org/wiki/Manual_talk:Image_admin ...

  8. [EWS]在exchange中的标识符

    摘要 最近在用ews的方式开发邮箱服务,包括写邮件,查看某封邮件的详情,回复,全部回复及转发功能.在获取收件箱的时候,关于唯一标识符的问题.也有点困惑,在每个邮件item中,存在一个changeKey ...

  9. ajax实例详解(2)

    说到ajax当然离不开json格式了.在ajax的数据传递过程中用到最多的便是json格式了吧. 什么是json格式,在刚开始用它的时候,着实让人迷茫了一阵子呀.说的白了点所谓json不就是一种数据的 ...

  10. 别老扯什么Hadoop了,你的数据根本不够大

    本文原名“Don't use Hadoop when your data isn't that big ”,出自有着多年从业经验的数据科学家Chris Stucchio,纽约大学柯朗研究所博士后,搞过 ...