题目链接

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 题意:有n段木棍,要求将这n根木棍拼成x跟长度相同的木棍,要使新的木棍尽量短,输出最小值? 思路:搜索,新的木棍的长度一定大于等于原来木棍的最大值maxlen,小于等于这些木棍的总长度sum,所以从小到大(maxlen~sum)遍历这些值,如果sum%i!=0 ,则肯定不能拼成合法的木棍直接跳过;
如果sum%i==0,那么有可能满足,则进行搜索。搜索过程:一根一根的深搜,记录当前已经拼成几根木棍,当前拼的这跟木棍还差多长,用过的木棍用v[i]进行标记。 代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int start,sum;
int v[]; int check(int num,int rest,int pos)
{
if(num==sum/start) return ;
rest-=pos; v[pos]--;
if(rest==)
{
int i;
for(i=; i>=; i--) if(v[i]) break;
int flag=check(num+,start,i);
v[pos]++;
return flag;
}
for(int i=rest; i>=; i--)
{
if(!v[i]) continue;
int flag=check(num,rest,i);
if(flag) return ;
}
v[pos]++;
return ;
} int main()
{
int n;
while(scanf("%d",&n)&&n)
{
int maxn=-;
sum=;
memset(v,,sizeof(v));
for(int i=; i<=n; i++)
{
int x; scanf("%d",&x);
sum+=x;
v[x]++;
maxn=max(maxn,x);
}
for(start=maxn; start<=sum; start++)
{
if(sum%start!=) continue;
if(check(,start,maxn))
{
printf("%d\n",start);
break;
}
}
}
return ;
}
/**
9
21 16 33 36 19 1 35 6 47
ans=107 43
46 16 47 31 22 48 10 47 25 48 33 31 35 33 14 21 8 22 20 37 20 48 8 18 3 44 28 16 9 50 44 18 46 28 43 49 18 19 31 46 3 43 43
ans=141
*/ /**
20
4 2 1 6 6 5 6 9 1 0 6 9 0 4 8 3 2 1 1 6
20
6 8 9 4 5 10 6 5 8 5 5 7 9 6 3 10 3 1 9 9
*/

poj 1011--Sticks(搜索)的更多相关文章

  1. POJ 1011 sticks 搜索

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 125918   Accepted: 29372 Descrip ...

  2. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  3. DFS(剪枝) POJ 1011 Sticks

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

  4. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  5. OpenJudge 2817:木棒 / Poj 1011 Sticks

    1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...

  6. POJ 1011 Sticks 【DFS 剪枝】

    题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

  7. POJ 1011 Sticks(搜索 && 剪枝 && 经典)

    题意 : 有n根木棍(n<=64),它们由一些相同长度的木棍切割而来,给定这n根木棍的长度,求使得原来长度可能的最小值. 分析 : 很经典的深搜题目,我们发现答案只可能是所有木棍长度总和的因数, ...

  8. poj 1011 Sticks (DFS+剪枝)

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

  9. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

  10. POJ 1011 Sticks(dfs+剪枝)

    http://poj.org/problem?id=1011 题意:若干个相同长度的棍子被剪成若干长度的小棍,求每根棍子原来的可能最小长度. 思路:很经典的搜索题. 我一开始各种超时,这题需要很多剪枝 ...

随机推荐

  1. jQuery防京东浮动网站楼层导航代码

    jQuery防京东浮动网站楼层导航代码   <!DOCTYPE html > <html xmlns="http://www.w3.org/1999/xhtml" ...

  2. python实战第一天-环境的安装

    操作系统 Ubuntu 15.10 IDE & editor JetBrains PyCharm 5.0.2 ipython3 Python版本 python-3.4.3 安装Python s ...

  3. 安徽省2016“京胜杯”程序设计大赛_B_阵前第一功

    阵前第一功 Time Limit: 1000 MS Memory Limit: 65536 KB Total Submissions: 63 Accepted: 29 Description A国每个 ...

  4. hexdump命令的使用

    hexdump命令是Linux下的打印16进制的利器,它可以按我们指定的格式输出16进制,特别有用,配合eeprom来用简直是绝配. 今天我们来介绍一个hexdump命令的使用: 首先我们准备一个测试 ...

  5. JavaScript中的EcMAScript学习笔记

    一.Javascript概述(知道)    a.一种基于对象和事件驱动的脚本语言    b.作用: 给页面添加动态效果    c.历史: 原名叫做livescript.W3c组织开发的标准叫ECMAs ...

  6. Qt5.8以上版本编译Oracle数据库的OCI驱动教程

    在前一篇的文章中我已经发过一个相似的文章,详情请点击:Qt5编译oracle驱动教程. 在那一篇文章中已经可以解决了Qt5的常用版本的Oracle数据库驱动的支持,但是在新的Qt开发工具中那种方法竟然 ...

  7. Git版本控制之多人协作

         上篇文章我们主要简单的介绍了有关git的一些基本常识和一些简单的命令.但那终究是皮毛,我们使用git最主要的目的还是管理我们的项目,多人协作.本篇文章主要涉及以下两个大模块: 分支的概念及原 ...

  8. Linux/Unix监控其他用户和信号

    --Linux/Unix监控其他用户和信号 ------------------------------------------------------2013/10/27   查看有哪些用户登录 w ...

  9. javaweb 登陆注册页面

    视图的数据修改,表中也修改引用工具类用<%@ page import=""%> <%@ page import="java.util.Date" ...

  10. Android 划屏切换调用finish()方法闪屏问题

    找了许多资料,偶然发现有种解决方法,就是修改style.xml里的Theme,有些NotitleBar,透明主题直接就能将此问题解决 原理也很简单,如果透明,背景色既为透明色,调用finish()时虽 ...