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个长度的短棒,是由原来等长的小棒任意剪切而来,问原来的小棒最短的长度是多少

【思路】用dfs搜索,重点是其中的几个剪枝,不减的话会超时。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=;
int n;
int d,num;
int val[N];
int vis[];
bool flag;
int cmp(int x,int y)
{
return x>y;
}
void dfs(int k,int len,int num1)
{
if(flag==) return ;
if(len==d)
{
dfs(,,num1+);
if(flag==) return ;
}
if(num1==num)
{
flag=;
return ;
}
for(int i=k;i<=n;i++)
{
if(!vis[i]&&len+val[i]<=d)
{
vis[i]=;
dfs(i+,len+val[i],num1);
vis[i]=;
if(len==) return ;

//如果当前搜索时,前面的长度为0,而第一根没有使用,说明第一根始终要废弃,这种组合必然不会成功

            if(len+val[i]==d) return ;
if(val[i]==val[i+]) i++;

//如果当前和上一次搜到的木棒一样长,就没必要再搜了

        }
} }
int main()
{
while(~scanf("%d",&n),n)
{
int sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
sum+=val[i];
}
sort(val+,val++n,cmp);
for(int i=val[];i<=sum;i++)
{
if(!(sum%i))
{
flag=;
memset(vis,,sizeof(vis));
d=i;
num=sum/d;
dfs(,,);
if(flag==) break;
}
}
printf("%d\n",d);
} return ;
}

Sticks_dfs的更多相关文章

随机推荐

  1. 你不知道的JavaScript-- 事件流与事件处理

    转载:http://blog.csdn.net/i10630226/article/details/48970971 1. 事件处理 1.1. 绑定事件方式 (1)行内绑定 语法: //最常用的使用方 ...

  2. 190. Reverse Bits -- 按位反转

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  3. css3导航-磊哥

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  4. Xmanager如何连接图形化界面

    1.编辑gnome配置文件vim /etc/gdm/custom.conf # GDM configuration storage [daemon]RemoteGreeter= /usr/libexe ...

  5. ASP.NET MVC统一异常处理

    前言: 今早看了篇文章:求知成瘾,却无作品 的思考:很有感触,发现原来自己也是这样,对每样东西都抱有很大的兴趣或者希望自己去学,一年后发现原来自己什么都是皮毛什么都不精!最终发现真正的大牛都是在某一个 ...

  6. 找出html中的图片、包括css中的图片,读出图片数据转换为base64数据

    <?php echo ">> 图片的地址,css里面的要打单引号\r\n"; echo ">> 相同的图片,使用css实现图片地址只出现一次 ...

  7. 2014北邮新生归来赛解题报告d-e

    D: 399. Who Is Joyful 时间限制 3000 ms 内存限制 65536 KB 题目描述 There are several little buddies standing in a ...

  8. c# MVC在WEB.Config中配置MIME

    在IIS中,默认没有添加.json格式的MIME,所有无法读取服务器中的.json格式的文件,返回结果404 方式一:在IIS中手动添加MIME 1.点击MIME进入MIME列表 2.添加MIME 3 ...

  9. 合理利用 vs2013的性能分析跟诊断

    选择对应的项目==> 我正常是选择采样 就包括里面的一些耗时.  挺好用的. 可以根据热路径 还有访问的占比.知道哪个环节占用的访问时间 还有性能耗能多. 可以点进去 跟踪跟修改

  10. Oracle中varchar,varchar2,nvarchar,nvarchar2的区别

    --varchar,varchar2 联系:1.varchar/varchar2用于存储可变长度的字符串比如varchar(20),存入字符串'abc',则数据库中该字段只占3个字节,而不是20个字节 ...