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. 【译文】 GC 安全点 和安全区域

    原文链接 : here 根引用  Root references  一个实例死了,意味着它变得无用.只用程序员知道一个实例是否已经无用.为了让程序知道一个实例是否已经无用,我们可以使用编译器分析,引用 ...

  2. Unix/Linux进程间通信(二):匿名管道、有名管道 pipe()、mkfifo()

    1. 管道概述及相关API应用 1.1 管道相关的关键概念 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管 ...

  3. 新语言代码高亮及Windows Live Writer插件开发

    最近在博客园做一些学习笔记.一个是看apple的swift官方书,另外一个是随学校课堂(SICP)学习scheme. 这两种语言都谈不上普及(或者说swift太新).博客园原来的windows liv ...

  4. WebService -- Java 实现之 CXF ( 使用CXF工具生成client 程序)

    1. 下载CXF 工具解压到磁盘 2.添加工具bin目录到PATH环境变量 3.创建一个CXF client新项目 4. run -> cmd 到指定目录,并运行工具目录下的批处理 “wadl2 ...

  5. AdminLTE 2 开源模版

    AdminLTE  2 开源模版: 1. 文档  https://almsaeedstudio.com/themes/AdminLTE/documentation/index.html 2. 代码   ...

  6. 找出只含有2,3,5因子构成的数的第N个

    https://leetcode.com/problems/ugly-number-ii/ 刚开始,一看题以为用刷选法,但是当数据量大时明显不行.然后感觉用含2,3,5因子的个数当做进制来处理还是不行 ...

  7. (转)CDN——到底用还是不用?

    用CDN的七个理由 浏览器从服务器上下载css.js和图片等文件时都要和服务器连接,而大部分浏览器对同一个域名用于下载文件的并发连接数限制在4个,这意味着如果要下载第五个文件就必须等前四个文件中有一个 ...

  8. thinkphp全站静态页实现方法!

    1:在根目录下的全局index.php中加下面这行: define('HTML_PATH', './htm');//生成静态页面的文件位置 2:在项目的配置文件config.php中加下面这行: 'H ...

  9. MVC和Webform的比较和替换总结

    1.自定义控件,页面赋值可用HtmlHelper进行扩展 2.aspx的母版页可用Layout代替 3.webform的request,response方法在MVC中同样适应,只是类有点不同,例如表单 ...

  10. tp框架之数据添加

    1.数组添加 //$attr = array("Code"=>"n088","Name"=>"哈萨克族"); ...