Day2-G-Sticks-POJ1011
Input
Output
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5 简述:给你n个木棒及其长度,求其能组成的最短的相同长度的木棒的长度。
分析:既然是最短搜索,用DFS+回溯即可,找出其最大的木棒长度,其可能的长度在最大长与长度和之间,与F题不同,这题需要有三点剪枝才能AC
1.不重复访问。
2.若长度为a的木棒不满足题意,则大于等于它的都不会满足,例如:2 2 2 2 1,若第一个2不满足,则需要直接跳过到1.
3.因为我们将木棒降序排序,第一根(最大)必然会用上,如果第一根木棒未被用上直接剪枝退出即可
注意考虑其只合成一根木棒的情况,代码如下:
const int maxm = ; int vis[maxm], n, buf[maxm], tmp, len; bool comp(int a,int b) {
return a > b;
} bool dfs(int cur,int pos,int t) {
if(t == tmp)
return true;
if(cur == len)
return dfs(, , t + );
int pre = ;
for (int i = pos; i < n; ++i) {
if(!vis[i]) { // first
int nlen = cur + buf[i];
if(pre != buf[i] && nlen <= len) { // second
pre = buf[i];
vis[i] = ;
if(dfs(nlen,i+,t))
return true;
vis[i] = ;
if(!pos) //third
return false;
}
}
}
return false;
} int main() {
while(scanf("%d",&n) && n) {
int sum = ;
for(int i = ; i < n; ++i) {
scanf("%d", &buf[i]);
sum += buf[i];
}
sort(buf, buf + n,comp);
for (len = buf[]; len <= sum / ; ++len) { // 若大于sum/2必然只能合成一根木棒
if(sum % len)
continue;
tmp = sum / len;
memset(vis, , sizeof(vis));
if(dfs(,,))
break;
}
if(len > sum / )
printf("%d\n", sum);
else
printf("%d\n", len);
}
return ;
}
Day2-G-Sticks-POJ1011的更多相关文章
- python学习第二天 -----2019年4月17日
第二周-第02章节-Python3.5-模块初识 #!/usr/bin/env python #-*- coding:utf-8 _*- """ @author:chen ...
- DFS系列 POJ(自认为的讲解)
C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
- poj1011 Sticks (搜索经典好题)
poj1011 Sticks 题目连接: poj1011 Description George took sticks of the same length and cut them randomly ...
- 【poj1011】 Sticks
http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...
- POJ1011 Sticks
Description George took sticks of the same length and cut them randomly until all parts became at mo ...
- poj1011 Sticks(dfs+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110416 Accepted: 25331 Descrip ...
- poj1011 Sticks(DFS+剪枝)
题目链接 http://poj.org/problem?id=1011 题意 输入n根棍子的长度,将这n根棍子组合成若干根长度相同的棍子,求组合后的棍子的最小长度.这题是poj2362的加强版,思路与 ...
- poj1011 Sticks[剪枝题]
https://vjudge.net/problem/POJ-1011 此题很重要.★★★ 很欢(e)乐(xin)的一道搜索剪枝题..poj数据还是太水了,我后来想不出来剪枝方法了,就加了句掐了时间语 ...
随机推荐
- 「题解」Just A String
目录 题目 原题目 简易题意 思路及分析 代码 题目 原题目 点这里 简易题意 现定义一个合法的字符串满足将其打散并任意组合之后能够形成回文串. 给你 \(m\) 种字母,问随机构成长度为 \(n\) ...
- 《Web安全攻防 渗透测试实战指南》 学习笔记 (二)
Web安全攻防 渗透测试实战指南 学习笔记 (二) 第二章 漏洞环境及实践
- Perl 笔记
目录 Perl 学习 常用记录 基础 1. 运行perl 2. 字符串 3. 变量 4. 条件 5. 循环 6. 运算符 7. 时间日期 8. 子程序(函数) 9. 引用 10. 格式化输出 11. ...
- Keras入门——(1)全连接神经网络FCN
Anaconda安装Keras: conda install keras 安装完成: 在Jupyter Notebook中新建并执行代码: import keras from keras.datase ...
- 【Hibernate HQL】
HibernateHQL public class HibernateHQL { //演示聚集函数使用 @Test public void testSelect7() { SessionFactory ...
- for in 与for 与hasOwnProperty
在遍历一个对象的时候我们会使用到for in属性. 现有对象和数组如下: var filght = { number: 1, status: 'watit', arrival: [1,2,3], ad ...
- MATLAB的安装与入门
最近安装了MATLAB来用,过程遇到很多问题,担心自己改天如果换电脑了就忘记一些安装问题,所以记录一个. 首先是资源问题,我在贴吧找到了好心人分享的破解资源(非常感谢好心人的资源(ง •_•)ง),然 ...
- [排错] SpringBoot 警告 Could not find acceptable representation
环境 Java 1.8 SpringBoot 2.1.9 Java 接口代码 @ResponseBody @RequestMapping(value = "cloud", meth ...
- javascript 变量、常量 、 函数 声明
声明变量: 方式一: 使用 var 定义变量,可在定义的同时赋值 或 不赋值 . 方式二: 直接使用[变量名 = 值]的形式,这会定义一个全局变量,但在严格模式下会出现引用错误.[不建议使用] 方式三 ...
- ios中为视图添加圆角
1.使用 layer设置指定圆角 或者设定一个或几个圆角 码修 关注 2017.04.20 19:03* 字数 107 阅读 656评论 0喜欢 0 由于项目中需要给按钮左下 和左上加圆角,我司可爱的 ...