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

Source

中文:

Description

乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位。然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度。请你设计一个程序,帮助乔治计算木棒的可能最小长度。每一节木棍的长度都用大于零的整数表示。

Input

输入包含多组数据,每组数据包括两行。第一行是一个不超过64的整数,表示砍断之后共有多少节木棍。第二行是截断以后,所得到的各节木棍的长度。在最后一组数据之后,是一个零。

Output

为每组数据,分别输出原始木棒的可能最小长度,每组数据占一行。

Sample Input

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output

6
5

Source

Translator

北京大学程序设计实习, Xie Di
 
 

正解:搜索+各种强力剪枝

解题报告:

  明天就要去北京参加夏令营了,今晚练一练手感。
  一直知道这道经典搜索题,然而一直没有打。今天打了一下,高兴地一遍过样例,然后交一发高兴地TLE。于是自己yy出了几个剪枝,调了一下,感觉快了很多,然而交了之后还是TLE,

  无奈上网蒯剪枝,发现如果当前需要搜一根新棒并且当前最长的那根用进去不可以那么可以直接return,这个强力剪枝让我直接AC了。

  调了1个多小时。。。

  其他的剪枝还是比较好想到的吧。

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
int n;
int a[MAXN];
int ans,total;
int ljh;
bool ok;
bool use[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline bool cmp(int q,int qq){ return q>qq; } inline void Init(){
total=;
for(int i=;i<=n;i++) a[i]=getint(),total+=a[i];
sort(a+,a+n+,cmp);
ans=a[]; ok=false;
memset(use,,sizeof(use));
} inline void dfs(int now,int len,int zong){
if(zong==n) { ok=; return ; }
int cun=-;//上一个不可行的值
for(int i=now;i<=n;i++) {
if(use[i] || cun==a[i]) continue;
use[i]=;
if(a[i]+len<ans){
dfs(i,a[i]+len,zong+);
if(ok) return ;
else cun=a[i];
}
else if(a[i]+len==ans){
dfs(,,zong+);//从0开始
if(ok) return ;
else cun=a[i];
}
use[i]=;
if(len==) break;//构建新棒时,对于新棒的第一根棒子,在搜索完所有棒子后都无法组合,则说明该棒子无法在当前组合方式下组合,不用往下搜索(往下搜索会令该棒子被舍弃),直接返回上一层
}
} inline void solve(){
while(scanf("%d",&n)!=EOF) {
if(n==) break;
Init();
for(;ans<=total-ans;ans++) //只需判断到一半
if(total%ans==) {
dfs(,,);
if(ok) break;
}
if(ok)
printf("%d\n",ans);
else printf("%d\n",total);
}
} int main()
{
solve();
return ;
}

POJ1011 Sticks的更多相关文章

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

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

  2. poj1011 Sticks(dfs+剪枝)

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

  3. poj1011 Sticks(DFS+剪枝)

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

  4. poj1011 Sticks[剪枝题]

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

  5. poj-1011 sticks(搜索题)

    George took sticks of the same length and cut them randomly until all parts became at most 50 units ...

  6. poj1011 Sticks (dfs剪枝)

    [题目描述] George took sticks of the same length and cut them randomly until all parts became at most 50 ...

  7. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  8. 【poj1011】 Sticks

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

  9. POJ-1011(sticks,深搜)

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

随机推荐

  1. [3D跑酷] AudioManager

    Unity音频管理 游戏中的声音管理最常用的组件莫过于AudioSource和AudioClip,我的做法是建立是一个AudioManager类(单例类)管理各个音频,谈一下我的经验: 函数列表 St ...

  2. java7-4 继承的练习

    1.Override和Overload的区别,Overload能改变返回值类型吗? 答: Override就是方法重写:在子类中,出现和父类中一模一样的方法声明的现象 Overload就是方法重载:在 ...

  3. openstack中彻底删除计算节点的操作记录

    在使用openstack的过程中,我们经常会添加好几台计算节点来部署虚拟机,在后续使用中由于某些原因,一些计算节点出现了问题,需要将这些出了问题的计算节点从openstack的控制节点中踢出去!但是很 ...

  4. Linux下squid代理缓存服务环境部署

    代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载 ...

  5. Gitub

    1.下载地址(注册:jackchn,jackchn@foxmail.com) http://windows.github.com/ 2.使用 github for Windows使用介绍 搭建一个免费 ...

  6. ABP入门系列——使用ABP集成的邮件系统发送邮件

    ABP中对邮件的封装主要集成在Abp.Net.Mail和Abp.Net.Mail.Smtp命名空间下,相应源码在此. #一.Abp集成的邮件模块是如何实现的 分析可以看出主要由以下几个核心类组成: E ...

  7. 区块链技术(一):Truffle开发入门

    以太坊是区块链开发领域最好的编程平台,而truffle是以太坊(Ethereum)最受欢迎的一个开发框架,这是我们第一篇区块链技术文章介绍truffle的原因,实战是最重要的事情,这篇文章不讲原理,只 ...

  8. 从0开始学Java——JSP&Servlet——HttpServletRequest相关的几个路径信息

    在HttpServletRequest中有几个获取路径的接口:getRequestURI/getContextPath/getServletPath/getPathInfo 这些接口互相之间有什么区别 ...

  9. [CareerCup] 7.5 A Line Cut Two Squares in Half 平均分割两个正方形的直线

    7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in ha ...

  10. django字段设置null和blank的区别

    null 这个选项跟数据库有关. null=True的话,数据库中该字段是NULL,即允许空值:null=False(默认)的话,数据库中该字段是NOT NULL,即不允许空值. blank 这个选项 ...