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. GCC参数详解-(转自:篱笆博客)

    gcc and g++分别是gnu的c & c++编译器 gcc/g++在执行编译工作的时候,总共需要4步 1.预处理,生成.i的文件[预处理器cpp] 2.将预处理后的文件不转换成汇编语言, ...

  2. JavaScript 实现简易版贪吃蛇(Day_13)

    时光永远在变迁,你始终要丢下过去. 使用语言 JavaScript  概述 运用JavaScript  实现简易版<贪吃蛇>.     Html 页面 1 <!DOCTYPE htm ...

  3. 使用Apache Pulsar + Hudi构建Lakehouse方案了解下?

    1. 动机 Lakehouse最早由Databricks公司提出,其可作为低成本.直接访问云存储并提供传统DBMS管系统性能和ACID事务.版本.审计.索引.缓存.查询优化的数据管理系统,Lakeho ...

  4. 夜晚场景图像ISP增强算法

    夜晚场景图像ISP增强算法 输入输出接口 Input: (1)图像视频分辨率(整型int) (2)图像视频格式(RGB,YUV,MP4等) (3)摄像头标定参数(中心位置(x,y)和5个畸变 系数(2 ...

  5. 现代传感器的接口:中断驱动的ADC驱动程序

    现代传感器的接口:中断驱动的ADC驱动程序 Interfacing with modern sensors: Interrupt driven ADC drivers 研究了如何编写一个阻塞的模数转换 ...

  6. vue3.0的变化

    初涉vue3.0,下面是我在demo中遇到的一些问题(我是用的vue-cli进行开发) [1]main.js中配置  第一个变化 vue2.x ===  Vue.prototype.$baseURL= ...

  7. 阿里云视频云 Retina 多媒体 AI 体验馆开张啦!

    带你体验视频更多可能 海量视频管理难度大?翻库检索特定人物费时费力?视频内容剪辑效率低?您的得力助手"Retina多媒体AI"体验馆已上线.带你感受视频AI黑科技,开启极致智能体验 ...

  8. 【NX二次开发】创建老版的基准平面uf5374

    使用uf5374() 源码: double dP1[3] = { 0.0,0.0,0.0 }; double dP2[3] = { 0.0,1.0,0.0 }; double dP3[3] = { 0 ...

  9. oracle数据库归档日志量陡增分析

    ============= oracle数据库archivelog暴增分析 ==================== 前言 归档量突然增长到981G/天,导致归档目录使用率告警 归档日志量异常暴增会导 ...

  10. Spring Boot WebFlux-10——WebFlux 实战图书管理系统

    前言 本篇内容我们会实现如下图所示的城市管理系统,因为上面案例都用的是 City,所以这里直接使用城市作为对象,写一个简单的城市管理系统,如图所示: 结构 类似上面讲的工程搭建,新建一个工程编写此案例 ...