Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 126238   Accepted: 29477

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
uva上面提交超时,poj上可以过;
AC代码:
 #include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
const int N = 1e4+;
int len[N],sum,L,T;
int used[N];
//bool cmp(int x,int y)
//{
// if(x>y) return true;
//}
int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
bool DFS(int m,int left)//m为剩余的木棒数,left为当前正在拼接的木棒和假定的木棒长度L还缺少的长度
{
if(m == && left == )
return true;
if(left == )//一根刚刚拼完
left = L;
for(int i=; i<T; i++)
{
if(!used[i] && len[i]<=left)
{
if(i>)//如果前者已经出现过的不能用,则当前的也不能用
{
if(!used[i-] && len[i] == len[i-])
continue;
}
used[i] = ;
if(DFS(m-,left-len[i]))
return true;
else
{
used[i] = ;
if(len[i] == left || left == L)
return false;
}
}
}
return false;
}
int main()
{
while(scanf("%d",&T) && T)
{ sum = ;
for(int i=;i<T;i++)
{
scanf("%d",&len[i]);
sum = sum + len[i];
}
//sort(len,len+T,cmp); sort 超时
qsort(len,T,sizeof(int),cmp); //从大到小排序
for(L = len[];L<=sum/;L++)
{
if(sum%L) continue;
memset(used,,sizeof(used));
if(DFS(T,L))
{
printf("%d\n",L);
break;
}
}
if(L>sum/) printf("%d\n",sum);
}
return ;
}

poj 1011 Sticks的更多相关文章

  1. DFS(剪枝) POJ 1011 Sticks

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

  2. POJ 1011 - Sticks DFS+剪枝

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

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

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

  4. OpenJudge 2817:木棒 / Poj 1011 Sticks

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

  5. POJ 1011 Sticks 【DFS 剪枝】

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

  6. poj 1011 Sticks (DFS+剪枝)

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

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

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

  8. POJ 1011 Sticks(dfs+剪枝)

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

  9. POJ 1011 sticks 搜索

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

随机推荐

  1. Android—进度条

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  2. PHP安全配置

    vi /etc/php.ini #编辑 date.timezone = PRC #在946行把前面的分号去掉,改为date.timezone = PRC disable_functions = pas ...

  3. 安装了iis之后,打开默认网站http://localhost/要求输入用户名和密码解决办法

        开始-运行gpedit.msc回车.     计算机配置--管理模板-windows 组件-Internet Exporer-Internet控制面板-安全页-Internet区域:双击登陆选 ...

  4. Java Web学习(1): 客户端请求、服务器响应及其HTTP状态码

    一JSP客户端请求 当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信 息头的一部分来传送的.我们可以查阅HTTP协议来获得更多的信息. 下表列出了浏 ...

  5. matlab:对一个向量进行排序,返回每一个数据的rank 序号 。。。

    %% Rank the entropy_loss     % for iiii = 1:size(Group_age, 1)  %     count_1 = 0 ;%     tmp = Group ...

  6. JSBinding + SharpKit / 常见问题

    运行时出现: Return a "System.Xml.XmlIteratorNodeList" to JS failed. Did you forget to export th ...

  7. CENTOS 下编译HTK

    在centenos下编译HTK碰到缺少libX11库,需要安装 libX11-dev libXext-dev libXtst-dev

  8. 将HTML段赋值给PHP变量的便捷方法,不使用转义字符

    <?php $b='12'; $a=<<<sss <html> <head> </head> <body> <i>& ...

  9. jQuery easyui datagrid数据绑定

    1.绑定json数据 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&qu ...

  10. CorelDRAW中拆清除调和效果的技巧

    图形对象应用的调和效果达不到用户的满意,可以将该种调和效果清除,清除调和效果后,只保留起始对象和结束对象.CorelDRAW软件支持两种方法来清除调和对象,本教程将详解CorelDRAW中清除调和效果 ...