Sticks

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

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
 
题意:
若干个棍子被切成若干份小棍子,问怎样将这些小棍子组成一些长度最小的一样长的小棍子。
代码:
 //DFS+剪枝;先将小木棍从大到小排序,从最长的小木棍开始枚举长度,dfs找到满足的就是最小的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,num,len,sum;
struct stick
{
int length,mark;
};
stick sti[];
bool cmp(stick x,stick y)
{
return x.length>y.length;
}
int dfs(int cnt,int now,int id) //cnt已经组成的小木棍,now正在组的小木棍的长度,id下标.
{
if(cnt==num) return ; //找到了
for(int i=id;i<n;i++)
{
if(sti[i].mark) continue; //用过了
if(now+sti[i].length==len)
{
sti[i].mark=;
if(dfs(cnt+,,))
return ;
sti[i].mark=;
return ;
}
else if(now+sti[i].length<len)
{
sti[i].mark=;
if(dfs(cnt,now+sti[i].length,i+))
return ;
sti[i].mark=;
if(now==) return ;//如果这一组当前的第一个没有被标的木棍与其他的任意都不能组成目标长度则这个木棍不能用,这种方案不行
while(sti[i].length==sti[i+].length)//加上这个小木棍不行,那么后面和这个小木棍一样长的就不用考虑了
i++;
}
}
return ;
}
int main()
{
while(scanf("%d",&n)&&n)
{
sum=;
for(int i=;i<n;i++)
{
scanf("%d",&sti[i].length);
sum+=sti[i].length;
sti[i].mark=;
}
sort(sti,sti+n,cmp);
for(int i=sti[].length;i<=sum;i++)
{
if(sum%i) continue;//能整除时才能正好分成num份不剩余。
num=sum/i;
len=i;
if(dfs(,,))
{
printf("%d\n",len);
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. Java 判断整数方法

    今天写代码的时候突然想到要怎么来判断整数,然后通过判断是否是整数来处理相关的操作.开始想到了几个方法,比如百度到的 x(int) instanceof Integer,但是这样的话程序会报错,还有一个 ...

  2. JavaScript -- 小试牛刀

    //var a = parseInt(window.prompt("请输入一个数字!","")); //switch(a) { // case 1 : // c ...

  3. HK&&CC JS学习:第一周——NO.2this

    1)常用的命名规范:     aXXXX:aBtn 说明获取的是一组元素:--类数组     oXXX:oBtn 说明获取的是一个元素->对象         对象有两个重要的特点:属性 和 方 ...

  4. serialize存入数组

    原代码 def get_type type_list = "" if categories.include?"movie" type_list += " ...

  5. PYTHON isinstance语法

    def obj_len(arg): #isinstance(),判断是否是某一类 if isinstance(arg,str) or (isinstance(arg,list)) or (isinst ...

  6. ACM/ICPC 之 电力网络-EK算法(POJ1459)

    按照电站发电(从源点到电站),消费者消费(从消费者到汇点)的想法构建网络,以下是EK解法 //网络流EK算法 //Time:922Ms memory:224K #include<iostream ...

  7. 「转」xtrabackup新版详细说明

    声明:本文由我的同事@fiona514编写,是我看过的最用心的中文说明介绍,强烈推荐大家学习使用. Percona Xtrabackup 2.4.1 编译及软件依赖 centos5,6 需要升级cma ...

  8. C#通过事件跨类调用WPF主窗口中的控件

    xaml.cs文件: using System; using System.Timers; using System.Windows; using System.Windows.Forms; name ...

  9. ABAP 客户报表

    *&---------------------------------------------------------------------* *& Report  ZSDR014 ...

  10. .NET LINQ 筛选数据

    筛选数据      筛选指将结果集限制为只包含那些满足指定条件的元素的操作. 它又称为选择. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表达式语法 更多信息 OfType ...