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. script "text/template"

    <script type="text/template" id="orgItem"> <div class="{orgClass}& ...

  2. PHP——字符串统一转码为GBK,自动判断是否UTF8并转码

    public static function strToGBK($strText) { $encode = mb_detect_encoding($strText, array('UTF-8','GB ...

  3. ORACLE 日期比较

    oracle sql日期比较:在今天之前: select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm ...

  4. 项目管理办公室 PMO

    项目管理办公室是组织中指导,协调,支持项目管理工作的一个常设职能部门,也就是管理项目管理的常设职能部门. 它负责指定和贯彻标准化的项目管理方法论(包括工作流程与规章制度等),协调所辖的各项目对资源,工 ...

  5. IISExpress配置文件的一个坑

    现象: 昨天在处理PBS系统问题的时候意外发现两个js错误(而同样的代码在同事机器上都没有问题),如下图. 图1 图2 图3 原因分析: 初步看起来是因为页面上没有id为'form1'的form和id ...

  6. 安卓/res/menu/的使用

    <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http:/ ...

  7. getParamValues()

    http://blog.csdn.net/msg_java2011/article/details/6529226

  8. 铁人系列(2)LA2218

    思路:对于每个人  都会有n-1个半片面  加上x>0,y>0,1-x-y>0(这里的1抽象为总长) 代码是粘贴的  原来写的不见了  orz............ // LA22 ...

  9. libpcap文件格式分析

    第一部分:PCAP包文件格式 一 基本格式: 文件头 数据包头数据报数据包头数据报...... 二.文件头: 文件头结构体  sturct pcap_file_header  {       DWOR ...

  10. MVC 3个重要的描述对象之ControllerDescriptor

    1.ControllerDescriptor 1.1 ReflectedControllerDescriptor public class HomeController : Controller { ...