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 ...
随机推荐
- hadoop2.7之作业提交详解(下)
接着作业提交详解(上)继续写:在上一篇(hadoop2.7之作业提交详解(上))中已经讲到了YARNRunner.submitJob() [WordCount.main() -> Job.wai ...
- 从IDEA角度来看懂UML图
前言 我们目前已经学习了设计模式的7种设计原则.下面本该是直接进入具体的设计模式系列文章. 但是呢在我们学习设计模式之前我们还是有必要了解一下uml图.因为后续的设计模式文章不出意外应该会很多地方使用 ...
- C语言编程入门之--第五章C语言基本运算和表达式-part4
5.3.5 和二进制极为密切的运算符 本小节的运算符需要借助二进制概念来理解. 二进制数据中,比如一个字节的数据,它的十进制为228,二进制就为11100100,如图5.11, 注意:如果不懂怎么转换 ...
- SpringMVC源码分析2:SpringMVC设计理念与DispatcherServlet
转自:https://my.oschina.net/lichhao/blog SpringMVC简介 SpringMVC作为Struts2之后异军突起的一个表现层框架,正越来越流行,相信javaee的 ...
- 使用CodeBenchmark对逻辑代码进行并发测试
一直对性能测试比较感兴趣,所以也写了不少的测试工具有WebApiBenchmark和TcpBenchmark等;但这些工具测试都是有针对性和配置的方式来进行功能有限所以很难适用更多的场景,所以单独开发 ...
- MSIL实用指南-加载int值
这一篇讲的是怎样加载整数值到运算栈上.这一类的指令都是以Ldc_I4开头. Ldc_I4类OpCodes的Ldc_I4字段的功能是把一个int值压入运算栈上.它的使用方法是ilGenerator.Em ...
- 原生js之Math对象
1.比较方法(常用) Math.min() //求一组数中的最小值 不能是数组,和对象等等. Math.max() //求一组数中的最大值eg:Math.min(5,3,5) // 3 2.取整(常用 ...
- Elasticsearch核心技术(2)--- 基本概念(Index、Type、Document、集群、节点、分片及副本、倒排索引)
Elasticsearch核心技术(2)--- 基本概念 这篇博客讲到基本概念包括: Index.Type.Document.集群,节点,分片及副本,倒排索引. 一.Index.Type.Docume ...
- Keras 实例 MNIST
import numpy from keras.datasets import mnist from keras.models import Sequential from keras.layers ...
- HDU - 2255 奔小康赚大钱 KM算法 模板题
HDU - 2255 题意: 分配n所房子给n个家庭,不同家庭对一所房子所需缴纳的钱是不一样的,问你应当怎么分配房子,使得最后收到的钱最多. 思路: KM算法裸题.上模板 #include <i ...