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. Mysql优化(出自官方文档) - 第九篇(优化数据库结构篇)

    目录 Mysql优化(出自官方文档) - 第九篇(优化数据库结构篇) 1 Optimizing Data Size 2 Optimizing MySQL Data Types 3 Optimizing ...

  2. C++ 重载运算符(详)

    C++ 重载运算符 C 重载运算符 一重载函数 1例程 2备注 二重载运算符 11 二元运算符重载 11 一元运算符重载 111 -- 2备注 3 特殊运算符重载 31 号运算符 32 下标运算符 3 ...

  3. pip3 Traceback (most recent call last)错误调试

    百度找的答案不知道是否正确反正不适用于deepin,就手动调试了一下,结果修复了原理不清楚. 调试环境: OS:deepin 15.11 Python3-pip 错误提示: python@python ...

  4. Docker笔记(九):网络管理

    Docker的应用运行在容器中,其相互之间或与外部之间是如何通信的,涉及到哪些知识点,本文对相关内容进行整理.因网络这块牵涉的面较多,因此只从日常使用或理解的角度出发,过于专业的就不深入探讨了. 1. ...

  5. 阿里云(ecs服务器)使用1-安装Mongdb数据库以及远程部署

    1.下载mongodb 百度云盘连接 :链接:https://pan.baidu.com/s/1b-hTS0XHQKpatecFoumLxw  提取码:z9ax 并送上可视化工具:链接:https:/ ...

  6. Codeforces Round #465 &935C. Fifa and Fafa计算几何

    传送门 题意:在平面中,有一个圆,有一个点,问能在这个圆中围出最大的圆的圆心坐标和半径.要求这个最大圆不包含这个点. 思路:比较基础的计算几何,要分三种情况,第一种就是这个点在圆外的情况.第二种是点在 ...

  7. 牛客20347 SDOI2011计算器(bsgs

    https://ac.nowcoder.com/acm/problem/20347 这篇是为了补bsgs(北上广深算法). 题意: 1.给定y,z,p,计算Y^Z Mod P 的值:  2.给定y,z ...

  8. 牛客多校第六场 C Generation I 组合数学 阶乘逆元模板

    链接:https://www.nowcoder.com/acm/contest/144/C来源:牛客网 Oak is given N empty and non-repeatable sets whi ...

  9. CF - 652F Ants on a Circle

    题目传送门 题解: 先观察蚂蚁相撞, 可以发现, 如果我们将相撞的2个蚂蚁互换位置的话,蚂蚁相当于没有碰撞体积,直接穿过去了.所以我们可以直接计算出最终哪些位置上会有蚂蚁. 接下来就需要知道蚂蚁们的最 ...

  10. CF 450E Jzzhu and Apples 数学+模拟

    E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...