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

分析:

  • 先存大的棍子,对于每一次搜索,关心的状态有三个:现在拼的棍子是第几个,已经拼了多少了,上次拼的那根是序列中的第几根
  • 如果拼当前棍子的时候,拼了一个长度为a的棍子,发现失败了,那么之后就不用再尝试长度为a的棍子了
  • 如果现在要拼的棍子长度为0,然后发现有一个棍子试图去拼然后失败了,那么这条路子就是错的,因为最终肯定不会全部拼完整,
  • 同理,如果刚好拼好一根棍子,发现接下来再拼失败了,那么接下来也没办法成功拼完整的。
int a[100],v[100],n,cnt,len;

bool cmp(int a,int b)
{
return a>b;
}
bool dfs(int stick,int cab,int last)
{
if(stick == cnt+1)
return true;
if(cab == len)
return dfs(stick+1,0,1);//这里第一次忘记return了,好坑
int fail = 0;
for(int i=last;i<=n;i++)
{
if(!v[i]&&cab+a[i]<=len&&fail!=a[i])
{
v[i] = 1; if(dfs(stick,cab+a[i],i+1))
return true;
fail = a[i];
v[i] = 0;
//如上述最后两条所说
if(cab==0||cab+a[i]==len)
return false;
}
}
return false;
}
int main()
{
while(cin>>n,n)
{
int sum = 0,val = 0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
val = max(val,a[i]);
}
sort(a+1,a+1+n,cmp);
for(len = val;len<=sum;len++)
{
if(sum%len)continue;
cnt = sum/len;
//cout<<cnt<<endl;
memset(v,0,sizeof v);
if(dfs(1,0,1))break;
}
cout<<len<<endl;
}
return 0;
}

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. POJ 1011 Sticks(dfs+剪枝)

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

  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

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

随机推荐

  1. Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)

    传送门 题意 给出n个区间[l,r]及花费\(cost_i\),找两个区间满足 1.区间和为指定值x 2.花费最小 分析 先用vector记录(l,r,cost)和(r,l,cost),按l排序,再设 ...

  2. 51nod 1268【dp】

    思路: 很水的dp吧...问的都是能否组成.. 哦对,这里sum最大才2e7,不会是2e9所以放心dp #include<cstdio> #include <map> #inc ...

  3. hdu 1171 Big Event in HDU【生成函数】

    按套路列生成函数式子然后暴力乘,这样复杂度看起来非常大,但是可以动态维护最大值,这样就是O(能过)的了 仔细想想这个多项式暴力乘理解成背包dp也行? #include<iostream> ...

  4. AT2534 港湾設備 (Port Facility)

    洛谷 先膜一下Iscream巨巨 首先我们可以把题目转化为线段覆盖,如果两条线段相交(不算某一条完全在另一条里面的情况),那么这两条线段代表的集装箱就不能放到同一个栈里,我们在它们之间连一条边.如果图 ...

  5. 即时编译(JIT)

    即时编译(JIT : just-in-time compilation): 指计算机领域里,即时编译也被成为动态翻译,是一种通过在运行时将字节码翻译为机器码,从而改善字节码编译语言性能的技术 即时编译 ...

  6. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) M

    Description The marmots have prepared a very easy problem for this year's HC2 – this one. It involve ...

  7. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  8. matplotlib 绘图实例01:正弦余弦曲线

    该讲的实例结果如下图所示: 第01步:导入模块,并设置显示中文和负号的属性: import matplotlib.pyplot as plt import numpy as np plt.rcPara ...

  9. 对js 面对对象编程的一些简单的理解

    由简单开始深入: 最简单的 直接对象开始 var desen = { age:24, name:'xyf', job:'fontEnd', getName:function(){ console.lo ...

  10. 03.Java多线程并发库API使用2

    1.多个线程之间共享数据的方式探讨 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 2.如果每个线程执行的代 ...