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. php代码规范—2

    如何写出好的 PHP 代码? 在本文中,我们将探讨一些良好的编程习惯,这将帮助你避免代码中的缺陷. 1- 编写模块化代码 良好的PHP代码应该是模块化代码.PHP的面向对象的编程功能是一些特别强大的工 ...

  2. PHP安装模式cgi、fastcgi、php_mod比较

    先了解一下普通cgi的工作流程: web server收到用户请求,并把请求提交给cgi程序,cgi程序根据请求提交的参数作相应处理,然后输出标准的html语句返回给web server,web se ...

  3. CentOS6.3 编译安装LAMP(2):编译安装 Apache2.4.6

    Apache官方说: 与Apache 2.2.x相比,Apache 2.4.x提供了很多性能方面的提升,包括支持更大流量.更好地支持云计算.利用更少的内存处理更多的并发等.除此之外,还包括性能提升.内 ...

  4. .net WebServer例

    新建.asmx页面 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  5. word20161213

    journal queue / 日志队列 journal quota / 日志配额 junction point / 交叉点 KDC, Key Distribution Center / 密钥分发中心 ...

  6. cf593d

    题意:给出一个有n(n<=200000)的树形图,每条边有一个权值.有两种操作,1是将一个边的权值变小, 2是给定两点a,b和一个值y,用y(long long范围内)依次除以两点之间的路径上的 ...

  7. RAD Studio 2009-10Seattle IDE Fix Pack 5.94

    IDE Fix Pack 5.94 IDE Fix Pack is a collection of unofficial bug fixes and performance optimizations ...

  8. 浅谈Javascript事件模拟

    事件是用来描述网页中某一特定有趣时刻的,众所周知事件通常是在由用户和浏览器进行交互时触发,其实不然,通过Javascript可以在任何时间触发特定的事件,并且这些事件与浏览器创建的事件是相同的.这就意 ...

  9. Servlet 生命周期与web容器的关系

    servlet生命周期由web容器(如tomcat)管理,初始化一次,直到web容器关闭才会被销毁.1.servlet是单例多线程,每个请求过来容器都会启用一个新线程 2.servlet在容器中保持单 ...

  10. 【Java EE 学习 28 下】【Oracle面试题2道】【Oracle练习题3道】

    一.已知程序和数据 create table test1 (id int primary key, name ), money int); ,); ,); ,); ,); 要求根据下图写出相应的sql ...