Sticks

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

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

我做的第二道dfs题。。。

题意:给出n个小木棒的长度,他们是又数根长度相同的木棒剪短得来的,问没剪短之前的木棒长度最短是多少

题解:要把这些小木棒拼接成数根长度相同的长木棒。要用到搜索算法。但因为搜索算法时间复杂度较高,需要剪枝才行。可以先给小木棒从大到小排个序,最后拼成的长木棒长度肯定在小木棒长度最大值和所有小木棒加起来的长度之间(因为是拼起来的,不可能比原来小木棒还短)。然后判断小木棒长度总和与假设的长木棒原长是不是整除关系。是的话就按照这个假设长度搜索,看不能全部拼成。

 #include<bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int n, sumv, target, aim;//target表示目标的棍子个数,aim表示目标的棍子长度,sumv为所有木棍总长度
int stick[maxn], vis[maxn];//vis数组标记 ,stick存储输入的木棒长度
bool cmp(int a, int b) {
return a > b;
}
bool dfs(int cnt, int len, int pos) {
if(cnt == target) return true;//根数对上就返回 退出dfs
if(len == aim) return dfs(cnt+, , ); //当拼完一根后,继续拼下一根
for(int i = pos; i < n; i++) {//从大到小排序后,按顺序搜索
if(!vis[i] && len+stick[i] <= aim) {
vis[i] = ;
if(dfs(cnt, len+stick[i], i+)) return true;
vis[i] = ; //只有失败才会运行到下面,否则是直接返回的
if(len == ) return false; //如果第一根时失败 剪枝
while(i+ < n && stick[i+] == stick[i]) i++; //如果下一根长度跟当前的失败的长度一样,剪枝
}
}
return false;
}
int main() {
while(~scanf("%d", &n), n) {
sumv = ;
for(int i = ; i < n; i++) {
scanf("%d",&stick[i]);
sumv += stick[i];
}
sort(stick, stick+n, cmp);
int ans = ;
for(int i = stick[]; i <= sumv; i++) {//拼好后的木棒长度只会在stick[0]和总长度之间
if(sumv % i == ) {
memset(vis, , sizeof(vis));
aim = i;
target = sumv / aim;
if(dfs(, , )) {
ans = aim;
break;
}
}
}
printf("%d\n",ans);
}
return ;
}

hdu1455Sticks(经典dfs+剪枝)的更多相关文章

  1. poj 1011 :Sticks (dfs+剪枝)

    题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...

  2. poj1011(DFS+剪枝)

    题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...

  3. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  5. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  6. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  7. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  8. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. Jmeter实例计划(查询天气)

    查询计划(天气查询) 这是一个入门计划,仅使用jmeter进行查询功能的计划,可参考http://www.cnblogs.com/TankXiao/p/4045439.html有代码可下载.不过我还是 ...

  2. 解决vue-router中this.$router.push无法在新窗口中打开

    解决vue-router中this.$router.push无法在新窗口中打开 let routeData = this.$router.resolve({ path: '/consult', que ...

  3. 一个有意思的CSS样式库--BUTTONS

    我最近发现了一个有意思的CSS样式库,和Bootstrap相似,但是它是专门的一个高度可定制的,免费的并且开源的按钮CSS样式库,这里附上它的网址:http://www.bootcss.com/p/b ...

  4. 使用OrgChart插件生成家谱组织结构图

    1.orgchart插件: github地址:https://github.com/dabeng/OrgChart 2.前端代码: //1.加载树形数据:ajax请求获取json格式的数据(flag参 ...

  5. 【Linux资源管理】一款优秀的linux监控工具——nmon

    (一)nmon工具概述 nmon是以一个用来做linux服务器监控的工具,通过nmon,可以实现对以下参数的监控: --CPU使用率 --内存.交换空间使用率 --网络使用情况 --磁盘I/O,读写速 ...

  6. sql1999语法

    1.交叉连接 cross join 左右两个表进行组合,产生笛卡尔积累. 左边每一行分别于右表每一行数据匹配. 2.using using使用的前提是两个表右关联的字段需要对应,两个表的join查询. ...

  7. Swift_类和结构体

    Swift_类和结构体 点击查看源码 struct Resolution { var width = 0 var height = 0 } class VideoMode { var resoluti ...

  8. webpack报错Cannot read property 'presetToOptions' of undefined

    在学习react全家桶时,webpack首先报错,报错内容如下,最后我是因为没有全局安装webpack导致的报错,使用npm install webpack -g安装解决了这个问题.

  9. 正则表达式-Regular expression学习笔记

    正则表达式 正则表达式(Regular expression)是一种符号表示法,被用来识别文本模式. 最近在学习正则表达式,今天整理一下其中的一些知识点 grep - 打印匹配行 grep 是个很强大 ...

  10. 小工具:生成半透明背景色的 CSS 代码,不影响子元素透明度

    工具:http://leegorous.net/tools/bg-alpha.html 工具介绍:http://leegorous.net/blog/2010/07/29/generate-css-c ...