UVA-307-Sticks-dfs+剪枝
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 file contains blocks of 2 lines. The first line contains the number of sticks parts after cutting. The second line contains the lengths of those parts separated by the space. The last line of the file contains ‘0’. 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
题意:给出一个n代表n根小木棍的长度,将这n根小木棒随意组合拼凑起来的木棍的长度一样且最小。
思路:需要多处剪枝
- 木棍的长度一定会是这些所有木块总和的因数,而且木棍长度一定大于所有分割好的木棍中最长的木棍。
- 将木棍从大到小排序,因为这样可以快一点搜索出这个木棍的长度是否能够被拼出。
- 如果发现当前这个长度的木棍块没有办法和后面的木棍拼出来的话,那么如果i-1和当前的木棍长度相等的话就可以不判断这个木棍了。
- 如果拼起来的长度大于你所要的木棍长度的话,需要继续判断,因为可能和之后的可以拼在一起,所以这里需要分情况考虑。
但是我代码还没写出来。。。
UVA-307-Sticks-dfs+剪枝的更多相关文章
- Sticks(UVA - 307)【DFS+剪枝】
Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大 ...
- poj 1011 :Sticks (dfs+剪枝)
题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...
- UVa 307 - Sticks
Sticks [题目链接]:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category ...
- poj1011 Sticks(dfs+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110416 Accepted: 25331 Descrip ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- POJ 1011 / UVA 307 Sticks
中文题 (一般都比较坑) 思路:DFS (感谢学长的幻灯片) 这破题把我折腾惨了!!!搞了n天 // by Sirius_Ren #include <cstdio> #include &l ...
- 紫书 习题7-14 UVa 307(暴搜+剪枝)
这道题一开始我想的是在排序之后只在头和尾往中间靠近来找木块, 然后就WA, 事实证明这种方法是错误的. 然后参考了别人的博客.发现别人是直接暴搜, 但是加了很多剪枝, 所以不会超时. 我也想过这个做法 ...
- hdu 1145(Sticks) DFS剪枝
Sticks Problem Description George took sticks of the same length and cut them randomly until all par ...
- 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 相同长度的木棍不再搜索 ...
随机推荐
- node-express(1)建立post、get、跨域问题解决方案
首先下载express:npm i express let ess=require('express'); let app=ess(); let bodyParser=require('body-pa ...
- vue tabNav 点击
<template> <div class="content"> <header class="tab_nav"> < ...
- TS-接口
接口 TS的核心原则之一是对值所具有的结构进行类型检测 接口初探 function printLabel(labelledObj: { label: string }) { console.log(l ...
- 使用vue-cli脚手架和vue-router搭建项目(一)
之前做的项目一直比较简单,并没有引入整个路由库.今天准备练习下
- unity 打包Apk生成签名证书keystore
进行Android项目开发中想要将androidapp导出为apk的时候需要选择一个数字证书,即keystore文件(android.keystore),它用来对我们的APP进行签名,是导出APP的一 ...
- hive的数据压缩
hive的数据压缩 在实际工作当中,hive当中处理的数据,一般都需要经过压缩,前期我们在学习hadoop的时候,已经配置过hadoop的压缩,我们这里的hive也是一样的可以使用压缩来节省我们的MR ...
- window location跳转
"top.location.href"是最外层的页面跳转"window.location.href"."location.href"是本页面 ...
- hexo next主题深度优化(二),懒加载。
文章目录 tip:没有耐心的可以直接看:正式在hexo next中加入懒加载(最下面) 废话 背景 懒加载简单介绍 引入js 重点!敲黑板了!!! 完善懒加载函数 懒加载函数可配置的参数 正式在hex ...
- AsyncAwait 学习
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- [21]APUE:线程同步之记录锁(文件)
[a] 概念 建议锁:在遵循相同记录锁规则的进程/线程间生效,通常用于保证某个程序自身多个进程/线程间的数据一致性 强制锁:意在保证所有进程间的数据一致性,但不一定有效:如不能应对先 unlink 后 ...