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. itrator控制迭代次数

    <s:iterator value="diys" status="d" begin="0" end="10" st ...

  2. linux磁盘分区模式

    linux磁盘分区模式 模式一:MBR 1)主分区不超过四个 2)单个分区容量最大2TB 模式二:GPT 1)主分区个数"几乎"没有限制(原因:在GPT的分区表中最多可以支持128 ...

  3. oracle分区表知识

    在F5中查看执行计划的时候总是看到很多信息: range分区 执行计划中出现的: 分区表,按 n1 ,n2 分区 partition range single:访问单个分区 partition ran ...

  4. .htaccess 基础教程(四)Apache RewriteCond 规则参数

    Apache模块 mod_rewrite 提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.它支持每个完整规则可以拥有不限数量的子规则以及附加条件规则的灵活而且强大的URL操作机制.此UR ...

  5. URL转义

    在处理一些url的时候常常会出现一些让人意想不到的情况,比如:当图片的链接中有空格的时候, this.src='http://img.cits-sh.com/UploadImgs/beihaidao2 ...

  6. Nginx 限速模块一览

    为了保护服务器不被刷流量,或者业务方面的一些限制,需要做一些限速措施. 一.http 请求并发连接数模块:ngx_http_limit_conn_module 这个模块可以设置每个定义的变量(比如客户 ...

  7. C++基础知识(3)---new 和 delete

    学过c语言的人都知道,c语言中动态分配内存空间使用的是库函数malloc,calloc,realloc以及free.而c++中所使用的是关键字new和delete.如 动态分配 new  ,  撤销内 ...

  8. VS2013 预定义的宏

    Visual Studio 2013 预定义的宏 https://msdn.microsoft.com/zh-cn/library/b0084kay(v=vs.120).aspx 列出预定义的 ANS ...

  9. mapReduce编程之Recommender System

    1 协同过滤算法 协同过滤算法是现在推荐系统的一种常用算法.分为user-CF和item-CF. 本文的电影推荐系统使用的是item-CF,主要是由于用户数远远大于电影数,构建矩阵的代价更小:另外,电 ...

  10. python之路十

    协程协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程.协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时 ...