Sticks

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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 100;
int n;
int s[N];
int v[N];
int t, length; //个数 长度
bool cmp(int a, int b){
return a > b;
}
bool dfs(int k, int len, int pos){ //第k根 第k跟长度 下标 //程序出口
if(k == t) return true; // 进来的时候肯定是 k = t, len = 0, pos = ? 最后一根的长度肯定是刚好等于 length !! if(len == length) return dfs(k+1, 0, 0); //拼接下一根 for(int i = pos+1; i <= n; i++){
if(!v[i] && len+s[i] <= length){
v[i] = 1;
if(dfs(k, len+s[i], i)) return true; //这种情况可以 下面的就不用看了
v[i] = 0; // len+s[i] 这种情况不行
if(len == 0) return false; //拼接第 k 跟木棒的 第一节的时候 不能拼成 length return false
while(s[i] == s[i+1]){ //相同的就不用看了
i++;
}
}
}
return false;
}
int main(){
while(~scanf("%d", &n) && n){
int sum = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &s[i]);
sum += s[i];
}
sort(s+1, s+1+n, cmp); //降序
for(int i = s[1]; i <= sum; i++){
if(sum%i == 0){
memset(v, 0, sizeof v);
length = i;
t = sum / i; //组成的木棒个数
if(dfs(1, 0, 0)){//当前木棒数, 当前木棒数的长度, 下标
printf("%d\n", i);
break;
}
}
}
}
return 0;
}

hdu 1145(Sticks) DFS剪枝的更多相关文章

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

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

  2. poj1011 Sticks(dfs+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110416   Accepted: 25331 Descrip ...

  3. HDU 1455 Sticks(经典剪枝)

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

  4. hdu - 1072(dfs剪枝或bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...

  5. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  6. HDU 1175 连连看 (DFS+剪枝)

    <题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...

  7. hdu 1044(bfs+dfs+剪枝)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

  9. DFS(剪枝) POJ 1011 Sticks

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

随机推荐

  1. nginx 的访问日志切割

    1. 高级用法–使用 nginx 本身来实现 当 nginx 在容器里,把 nginx 日志挂载出来的时候,我们发现就不适合再使用 kill -USR1 的方式去分割日志这时候当然就需要从 nginx ...

  2. springboot 搭配redis缓存

    1.引入依赖 <dependencies> <dependency> <groupId>org.springframework.boot</groupId&g ...

  3. curl 常用操作总结

    前言 curl 是一个强大的命令行工具,支持 HTTP, HTTPS, SCP 等多种协议,本文主要总结一下其常用的功能,方便及时查阅. curl --version curl 7.68.0 (x86 ...

  4. 在NVIDIA A100 GPU中使用DALI和新的硬件JPEG解码器快速加载数据

    在NVIDIA A100 GPU中使用DALI和新的硬件JPEG解码器快速加载数据 如今,最流行的拍照设备智能手机可以捕获高达4K UHD的图像(3840×2160图像),原始数据超过25 MB.即使 ...

  5. 编译原理-确定有穷自动机(deterministic finite automata ,DFA)

    是一个五元组 M=(S,∑,f,S0,F) 其中 S:有穷状态集 ∑:输入字母表(有穷) f:状态转换函数.f(S,a)=S' 是单值部分映射,每个状态面临一个输入符号时,转入的后继状态是确定的. S ...

  6. Idea快捷键大全(Windows)

    Ctrl 快捷键 介绍 Ctrl + F 在当前文件进行文本查找 (必备) Ctrl + R 在当前文件进行文本替换 (必备) Ctrl + Z 撤销 (必备) Ctrl + Y 删除光标所在行 或 ...

  7. QT基本数据类型

    因为Qt是一个C++框架, 因此C++中所有的语法和数据类型在Qt中都是被支持的, 但是Qt中也定义了一些属于自己的数据类型, 下边给大家介绍一下这些基础的数类型. QT基本数据类型定义在#inclu ...

  8. 如何使用 Python 统计分析 access 日志?

    一.前言 性能场景中的业务模型建立是性能测试工作中非常重要的一部分.而在我们真实的项目中,业务模型跟线上的业务模型不一样的情况实在是太多了.原因可能多种多样,这些原因大大降低了性能测试的价值. 今天的 ...

  9. 重新整理 .net core 实践篇—————文件系统[二十二]

    前言 简单介绍一下文件系统. 正文 文件系统,主要是下面3个接口组成: IFileProvider IFileInfo IDirectoryContents 那么他们的实现是: physicalFil ...

  10. UF_MODL 建模操作

    Open C uc6560 uf5300uf5301uf5303uf5305uf5309uf5310uf5311uf5313uf5315uf5317uf5319uf5320uf5321uf5323uf ...