Problem Description

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 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.

Output

The output file contains the smallest possible length of original sticks, one per line.

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)的更多相关文章

  1. 【蓝桥杯/算法训练】Sticks 剪枝算法

    剪枝算法 大概理解是通过分析问题,发现一些判断条件,避免不必要的搜索.通常应用在DFS 和 BFS 搜索算法中:剪枝策略就是寻找过滤条件,提前减少不必要的搜索路径. 问题描述 George took ...

  2. poj1011 Sticks[剪枝题]

    https://vjudge.net/problem/POJ-1011 此题很重要.★★★ 很欢(e)乐(xin)的一道搜索剪枝题..poj数据还是太水了,我后来想不出来剪枝方法了,就加了句掐了时间语 ...

  3. LeetCode 笔记总结

    前言 之前把一些LeetCode题目的思路写在了本子上,现在把这些全都放到博客上,以后翻阅比较方便. 题目 99.Recover Binary Search Tree 题意 Two elements ...

  4. 2017 ICPC/ACM 沈阳区域赛HDU6223

    Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java ...

  5. 搜索 + 剪枝 --- POJ 1101 : Sticks

    Sticks Problem's Link:   http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...

  6. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  7. hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. poj1011 Sticks(dfs+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110416   Accepted: 25331 Descrip ...

  9. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

随机推荐

  1. 记一次 Windows MySQL 恢复

    0x00 事件 因为本地的服务器硬件出现故障,导致一台 Windows 系统的开发环境挂了,且无法短时间内恢复状态. 应急方案是使用了云上的系统重建了开发环境. 开发人员说需要挂了的那台 Window ...

  2. 本地VSCode编辑远程服务器文件

    前言 先说下我的场景:服务器搭设了一系列复杂环境,然后需要使用PHP实现某些功能 选这种远程编辑的原因: 首先PHP打死我也不想装(这个现在是出了VB外最惹人厌的语言了) 然后环境比较复杂,本地装下比 ...

  3. Zabbix-绘制动态拓扑图基础篇

    一.实验环境 1.1 zabbix 4.0.2 二.实验需求介绍 公司希望网络拓扑能够动态反应物理接口的状态或者业务的状态,希望将网络拓扑显示到大屏上 三.Zabbix在绘制拓扑的优缺点 3.1 优点 ...

  4. 熔断器Hystrix

    什么是服务雪崩? 单个服务发生故障,占用过多的系统资源,从而导致级联故障的情况称为服务雪崩. 什么是Hystrix? 在分布式环境中,许多服务依赖项中的一些必然会失败.(服务挂了) Hystrix是一 ...

  5. Gradle——创建简单的项目

    项目 & 任务 Gradle 的一切都是基于项目和任务的. 构建由一个或多个项目组成.项目的概念很抽象,它取决于你要用Gradle 做什么 .项目可以是 一个 Jar 库或者一个 web 程序 ...

  6. Feign详细构建过程及自定义扩展

    探究清楚 feign 的原理,自定义 feign 功能 **spring-cloud-openfeign-core-2.1.1.RELEASE.jar** 中 **HystrixFeign** 的详细 ...

  7. JavaScript浮点数运算的精度问题

    之前在做浮点数计算时,偶然发现计算结果有误差,度娘了解了下,补充整理了下. 误差是什么样子的呢?举例 console.log(0.1+0.2); // 0.30000000000000004 事实上在 ...

  8. 打包一沓开源的 C/C++ 包管理工具送给你!

    本文作者:HelloGitHub-ChungZH 博客地址:https://chungzh.cn/ 包管理器可以帮助你更方便地安装依赖关系,并决定所安装的版本,提高你的开发幸福感.许多语言都有自己的包 ...

  9. java IO流 之 FIle类基础

    package IO; import java.io.File;import java.io.IOException; public class FileIO { /** * 构建及获取文件名信息 * ...

  10. java学习之- 线程运行状态

    标签(空格分隔): 线程运行状态 线程的运行状态: 如下是是我编写的一个图,大家可以作为参考: 1.new一个thread子类也是创建了一个线程: 2.创建完毕之后start()-----运行, 3. ...