hdu 1145(Sticks) DFS剪枝
Sticks
Problem DescriptionGeorge 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.InputThe 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.OutputThe output file contains the smallest possible length of original sticks, one per line.Sample Input95 2 1 5 2 1 5 2 141 2 3 40Sample Output65
#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剪枝的更多相关文章
- poj 1011 :Sticks (dfs+剪枝)
题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...
- poj1011 Sticks(dfs+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110416 Accepted: 25331 Descrip ...
- HDU 1455 Sticks(经典剪枝)
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hdu - 1072(dfs剪枝或bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- HDU 1175 连连看 (DFS+剪枝)
<题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...
- hdu 1044(bfs+dfs+剪枝)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
随机推荐
- python文件处理(对比和筛选)版本2
场景:对比两个txt文件的差异,将对比结果写入html,将不同部分写入另一个txt #!/user/bin/python #!coding=utf-8 # -*- coding: utf-8 -*- ...
- IDEA 创建 Vue 文件(Day_41)
IDEA 创建 Vue 文件 1. 在setting-->plugins里安装vue插件,安装成功之后重启IDEA 如图 2. 在setting-->Editor-->File Ty ...
- 小程序中在设置了textarea后三个祖级内事件失效
在一次写小程序项目中收货地址中的详细地址时,我用的是文本域,下边的三个bindtap事件却不能使用了:下图: 报错信息如下图: 通过一番查找以及尝试之后,我发现是因为textarea标签的问题,但是依 ...
- mybatis运行出现org.apache.ibatis.binding.BindingException
今天学习mybatis的第一天,发现用junit测试报出了次异常:org.apache.ibatis.binding.BindingException: Type interface cn.dzp.d ...
- GO学习-(33) Go实现日志收集系统2
Go实现日志收集系统2 一篇文章主要是关于整体架构以及用到的软件的一些介绍,这一篇文章是对各个软件的使用介绍,当然这里主要是关于架构中我们agent的实现用到的内容 关于zookeeper+kaf ...
- GO语言面向对象05---接口的多态
package main import "fmt" type Fighter interface { Attack() (bloodloss int) Defend() } /*骑 ...
- springboot项目添加swagger2
1.pom中添加swagger依赖 <!-- swagger-ui --> <dependency> <groupId>io.springfox</group ...
- TVM将深度学习模型编译为WebGL
使用TVM将深度学习模型编译为WebGL TVM带有全新的OpenGL / WebGL后端! OpenGL / WebGL后端 TVM已经瞄准了涵盖各种平台的大量后端:CPU,GPU,移动设备等.这次 ...
- deeplearning模型量化实战
deeplearning模型量化实战 MegEngine 提供从训练到部署完整的量化支持,包括量化感知训练以及训练后量化,凭借"训练推理一体"的特性,MegEngine更能保证量化 ...
- 2020年Yann Lecun深度学习笔记(上)
2020年Yann Lecun深度学习笔记(上)