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. spring data flow

    spring data flow相当于一个快速发布应用的平台.并可以通过消息队列(kafa,rabbitMQ)把多个应用链接在一起进行链式处理数据.支持的平台是: Cloud Foundry Apac ...

  2. Maven学习笔记:POM标签大全详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. Spring Boot 集成 Swagger2 教程

    上篇讲过 Spring Boot RESTful api ,这篇简单介绍下 SwaggerUI 在 Spring Boot 中的应用. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可 ...

  4. mybatis源码探索笔记-1(构建SqlSessionFactory)

    前言 mybatis是目前进行java开发 dao层较为流行的框架,其较为轻量级的特性,避免了类似hibernate的重量级封装.同时将sql的查询与与实现分离,实现了sql的解耦.学习成本较hibe ...

  5. C语言-错误处理

    标记程序的运行状态和控制主要有以下几种:break/continue/return/参数的返回值/exit(int n)_exit() atexit((*p)(参数列表)): 1 break:用在开关 ...

  6. QWidget: “Must construct a QApplication before a QWidget”

    最近在做一个关于Qt的项目,在debug版本中没有任何问题,所以就想看看在Release版本下的运行情况,结果在开始运行时,出现如下图1-1所示的错误.在网上搜索答案,大多数是关于QWidget: M ...

  7. ie使用已安装的最高版本ie代码,及ie下不要在结尾处写多余的逗号“,”

    ie使用已安装的最高版本ie代码 <meta http-equiv = "X-UA-Compatible" content = "IE=7,IE=9" & ...

  8. AF(操作者框架)系列(1)-LabVIEW中的模块化应用概述

    一.引子 在前面对LabVIEW介绍的文章中,关于框架开发的内容涉及很少.为了讲解操作者框架(Actor Framework)的优缺点,也只是拿出来QDSM(Queue-Driven State Ma ...

  9. MySQL8.0 ROW_NUMBER、RANK、DENSE_RANK窗口函数 分组排序排名

    MySQL8.0 (ROW_NUMBER)窗口函数 排名 暂时理解函数意义,后面再进行优化,如果有关变量排序,查看这个大哥的 mysql的分组排序和变量赋值顺序 先查看一个例子: # 按照每科课程分数 ...

  10. 3分钟让你的Eclipse拥有自动代码提示功能

    第一步:Window->Preferences->Java 第二步:Java->Editor->Content Assist->Auto Activation->将 ...