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. 5.spring:注解配置 Bean

    在classpath中扫描组件 组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定的组件包括: ->@Componment:基于注解,标识一个受Spring管理的 ...

  2. ethereumjs/ethereumjs-account-1-简介和API

    https://github.com/ethereumjs/ethereumjs-account Encoding, decoding and validation of Ethereum's Acc ...

  3. ARM 汇编指令集 特点之一:条件执行后缀

    mov r0,r1; moveq r0,r1;//这句代码是否执行 取决于 上几句代码 的运行结果(最后一次CPSR 寄存器 的 高四位   NZCV 的标志位) 条件后缀执行特点:1.条件后缀执行是 ...

  4. oracle之DQL

    一.单表查询 语法:select * from table where 条件 group by 分组 having 过滤分组 order by 排序 --查询平均工资低于2000的部门的最大工资和平均 ...

  5. OCR

    谷歌OCR光学字符识别窥探 - 简书 Tesseract OCR初探 利用Tesseract图片文字识别初探 _ TonyDeng's Blog Tesseract OCR(光学字符识别)教程 - C ...

  6. ELK6.7.0 Windows 环境本地安装

    安装环境环境准备 第一次写博文,排版比较乱,理解万岁 ELK 6.7.0三件套 下载地址:https://www.elastic.co/cn/downloads/ windows环境下默认已配置jdk ...

  7. sysbench安装

    sysbench安装 1.下载软件mkdir -p /usr/local/softwarecd /usr/local/softwaregit clone https://github.com/akop ...

  8. python打印99乘法表

    代码如下: print(XXX,end="\t") #表示打印不换行 附带python部分转义字符:

  9. Linux基础(03)、常用基础指令和操作

    目录 一.什么是Linux 二.常用基础指令 2.1.vi编辑 2.2.Linux文件类型 2.3.常用指令:增.删.改.查.其他 三.Linux的目录和权限 3.1.目录 3.2.权限 3.3.修改 ...

  10. 多线程异步非阻塞之CompletionService

    引自:https://www.cnblogs.com/swiftma/p/6691235.html 上节,我们提到,在异步任务程序中,一种常见的场景是,主线程提交多个异步任务,然后希望有任务完成就处理 ...