Sticks(剪枝+BFS)
Problem Description
Input
Output
SampleInput
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
SampleOutput
6
5 题意就是给你一堆不同长度的木棍,要你用他们拼成长度相等的木棍,输出最短且满足要求的木棍长度。
就直接从0开始搜嘛,判断长度相同就输出,好了就这样,提交,TLE。
=7=那么看来不能直接搜索,如何优化呢,当然就是判断一些条件剪枝,首先是搜索范围,根据题意有原始长度肯定比现在最长的要长,而且能被现在总长除尽,原始长度最长肯定就是总长度(只有一根木棍),其次就是对于同一个长度的木棍,只尝试一次,一次不成功便不需要尝试第二次,然后就依次dfs就行了。
代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int s[];
bool vis[];
int sum,n; bool dfs(int pos, int res, int len){
if(pos == && res == )
return true;
if(!res)
res = len;
for(int i = n - ; i >= ; i--){
if(vis[i] || s[i] > res)
continue;
vis[i] = true;
if(dfs(pos - , res - s[i], len))
return true;
vis[i] = false;
if(res == s[i] || res == len) //判断是否尝试过
return false;
}
return false;
} int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
while(cin >> n && n){
sum = ;
for(int i = ; i < n; i++){
cin >> s[i];
sum += s[i];
}
sort(s, s+n);
int i;
for(i = s[n - ]; i <= sum/;i++){ //从最大长度开始dfs
if(sum % i) //小于总长度则dfs尝试
continue;
MMT(vis); //每次dfs前记得清空标记
if(dfs(n,i,i)){
cout << i << endl;
break;
}
}
if(i > sum/) //大于长度一半,则不可能被分成多个木棍,肯定只能由一根木棍构成
cout << sum << endl;
}
return ;
}
Sticks(剪枝+BFS)的更多相关文章
- 【蓝桥杯/算法训练】Sticks 剪枝算法
剪枝算法 大概理解是通过分析问题,发现一些判断条件,避免不必要的搜索.通常应用在DFS 和 BFS 搜索算法中:剪枝策略就是寻找过滤条件,提前减少不必要的搜索路径. 问题描述 George took ...
- poj1011 Sticks[剪枝题]
https://vjudge.net/problem/POJ-1011 此题很重要.★★★ 很欢(e)乐(xin)的一道搜索剪枝题..poj数据还是太水了,我后来想不出来剪枝方法了,就加了句掐了时间语 ...
- LeetCode 笔记总结
前言 之前把一些LeetCode题目的思路写在了本子上,现在把这些全都放到博客上,以后翻阅比较方便. 题目 99.Recover Binary Search Tree 题意 Two elements ...
- 2017 ICPC/ACM 沈阳区域赛HDU6223
Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
- 搜索 + 剪枝 --- POJ 1101 : Sticks
Sticks Problem's Link: http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- poj1011 Sticks(dfs+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110416 Accepted: 25331 Descrip ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
随机推荐
- java中安全的单例与不安全的单例
java中安全的单例与不安全的单例 1.内部静态类(安全的) public class Singleton { private static class SingletonHolder{ privat ...
- go 学习笔记之是否支持以及如何实现继承
熟悉面向对象的小伙伴们可能会知道封装,继承和多态是最主要的特性,为什么前辈们会如此看重这三种特性,真的那么重要吗? 什么是封装 什么是封装,封装有什么好处以及怎么实现封装? 相信大多数小伙伴们都有自己 ...
- PythonI/O进阶学习笔记_1.抽象、面向对象、class/object/type
前言: 是自己在学习python进阶IO学习视频的时候的理解和笔记,因为很多都是本菜鸟学习时候的自己的理解,有可能理解有误. Content: - 抽象的概念和面向对象的概念?想要大概了解python ...
- python 27 异常处理
目录 异常处理 1. 错误分类 2. 异常 3. 异常处理 4. 异常处理的两种方法 5. try的结构 5.1 结构一:单分支结构 5.2 结构二:多分支结构 5.3 结构三:万能异常 5.4 结构 ...
- 计算机基础+python初阶
今日内容: 1.计算机基础知识 2.python简介 3.快速入门 今日内容: 一.计算机基础 1. 计算机什么组成的 输入输出设备 cpu 硬盘 内存 中央处理器 处理各种数据 相当于人的大脑 内存 ...
- effective java 3th 序
正本基本是自己翻译,翻译绝对有错误,就是这么自信,看的时候,自己注意下,如果感觉有语句不通,那么可能就是我翻译的出现了问题,可以自己翻找原文对比下. 其中自己的见解,我写在脚注中. 在 1997 年, ...
- Asp.net之MsChart控件动态绑定温度曲线图
<div> <div style="position: absolute; z-index: 200; background-color: #FFFFFF; height: ...
- C#开发BIMFACE系列16 服务端API之获取模型数据1:查询满足条件的构件ID列表
系列目录 [已更新最新开发文章,点击查看详细] 源文件/模型转换完成之后,可以获取模型的具体数据.本篇介绍根据文件ID查询满足条件的构件ID列表. 请求地址:GET https://api.b ...
- JVM中class文件探索与解析
一直想成为一名优秀的架构师的我,转眼已经工作快两年了,对于java内核了解甚少,闲来时间,看看JVM,吧自己的一些研究写下来供大家参考,有不对的地方请指正. 废话不多说,一起来看看JVM中类文件是如何 ...
- 你真的了解MyBatis中${}和#{}的区别吗?
动态sql是mybatis的主要特性之一.在mapper中定义的参数传到xml中之后,在查询之前mybatis会对其进行动态解析. mybatis提供了两种支持动态sql的语法:#{} 和 ${}. ...