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 should contains the smallest possible length of original sticks, one per line.

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

  1. python学习第二天 -----2019年4月17日

    第二周-第02章节-Python3.5-模块初识 #!/usr/bin/env python #-*- coding:utf-8 _*- """ @author:chen ...

  2. DFS系列 POJ(自认为的讲解)

    C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...

  3. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  4. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  5. poj1011 Sticks (搜索经典好题)

    poj1011 Sticks 题目连接: poj1011 Description George took sticks of the same length and cut them randomly ...

  6. 【poj1011】 Sticks

    http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...

  7. POJ1011 Sticks

    Description George took sticks of the same length and cut them randomly until all parts became at mo ...

  8. poj1011 Sticks(dfs+剪枝)

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

  9. poj1011 Sticks(DFS+剪枝)

    题目链接 http://poj.org/problem?id=1011 题意 输入n根棍子的长度,将这n根棍子组合成若干根长度相同的棍子,求组合后的棍子的最小长度.这题是poj2362的加强版,思路与 ...

  10. poj1011 Sticks[剪枝题]

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

随机推荐

  1. toFixed的坑

    因为项目需要使用小数,所以第一时间想到了javaScript 的toFixed 方法,开始测试了一下,觉得没有问题就没管了,后来同事偶然间发现 toFixed 之后的数据不对,后来自己查阅资料才知道, ...

  2. Spring学习(八)

    AOP的重要概念 1.切面 : 切点(Pointcut) + Advice[ 在哪里 .加什么 ] 2.Advice: 在 切点 选中的 连接点 "加入" 的 代码 就是 Advi ...

  3. 推荐一款好用的博客离线编辑工具——OpenLiveWriter

    1.前言 我们自己一般在写博客的时候都是在博客官网后台写的,但是如果要在多个平台发布博客的话,那就要复制好前面写好的博客,然后再去其它博客平台发布,可见非常的麻烦. 这里推荐一款好用的离线多功能,多平 ...

  4. 【PAT甲级】1058 A+B in Hogwarts (20 分)

    题意: 输入两组,每组三个非负整数A,B,C(A<=1e7,B<17,C<29),输出相加的和.(类似个位上29进制,十位上17进制运算) AAAAAccepted code: #d ...

  5. 涂涂影院APP-免费VIP电影观看「安卓APP」

    最新下载链接:https://www.lanzous.com/u/niceyoo 2019年基本就没推广过这款APP,很失败,从第一版发布到现在涂涂影院已经做了2年了, 由于没有官网,所以基本百度能搜 ...

  6. 洛谷P2142 高精度减法 题解

    想找原题请点击这里:传送门 原题: 题目描述 高精度减法 输入格式 两个整数a,b(第二个可能比第一个大) 输出格式 结果(是负数要输出负号) 输入输出样例 输入 复制 输出 复制 说明/提示 %数据 ...

  7. 洛谷P1301 魔鬼之城 题解

    想找原题请点击这里:传送门 题目描述 在一个被分割为N*M个正方形房间的矩形魔鬼之城中,一个探险者必须遵循下列规则才能跳跃行动.他必须从(, )进入,从(N, M)走出:在每一房间的墙壁上都写了一个魔 ...

  8. 最全BT磁力搜索引擎,国外最受欢迎的BT-磁力网站(整理分享,每日不断更新...)

    最全BT磁力搜索引擎索引(整理分享,每日更新) 1.海盗湾 The Pirate Bay 2.磁力天堂(BT磁力搜索下载-磁力天堂) www.btaa.xyz  (资源多,下载速度可以,建议用手机访问 ...

  9. 利用DFS算出有多少个连通图

    以下面一个题目为例,[题目链接]: https://www.luogu.com.cn/problem/P4961 题目中涉及求出八联通图的个数,这里给出这步的代码: memset(vis, 0, si ...

  10. MSSQL2005数据库快照(SNAPSHOT)初探

    定义:数据库快照是数据库(称为“源数据库”)的只读静态视图.在创建时,每个数据库快照在事务上都与源数据库一致.多个快照可以位于一个源数据库中,并且可以作为数据库始终驻留在同一服务器实例上.在创建数据库 ...