POJ 1011 sticks 搜索

Sticks

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

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

Source

Central Europe 1995

这道题的剪枝到极致了。少一点也不行,很好的搜索题。我是看了网上的代码才A的,各种剪枝。

这个题如果A了,必须要深刻理解才行,关于各种剪枝,代码中有注释

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 70;
int n, arr[maxn], Sum;
bool vis[maxn];
/*cur表示当前选择棍子的序号,diff_len为这根棍子还差多长,
sum_len初始值为总长Sum, cur_len表示搜索当前棍子长度为cur_len的棍子 */
bool dfs(int cur, int diff_len, int sum_len, int cur_len)
{
    if (diff_len == 0)
    {
        sum_len -= cur_len;
        if (sum_len == 0)//判断是否所有的棍子都取完了
        {
            return true;
        }
        for (cur = 0; vis[cur]; cur++);//剪枝1,找到剩下的最长的一根,然后接着这个往下找比他小的
            vis[cur] = true;
        if (dfs(cur + 1, cur_len - arr[cur], sum_len, cur_len))//从最长的开始继续往下找 这个剪枝的威力很大,从不能TLE直接到A的差距
            return true;
        vis[cur] = false;
        sum_len += cur_len;
    }
    else
    {
        for (int i = cur; i < n; i++)
        {
            /*剪枝2,如果这个棍子的长度等于上一个棍子的长度,并且上一个还不符合要求,
            没有取,所以这个肯定也不能取,直接跳过 */
            if (i > 0 && arr[i] == arr[i - 1] && !vis[i - 1])
                continue;
            if (!vis[i] && diff_len >= arr[i])//如果没有取过,并且需要取的长度大于待取的才行,小小剪枝3,最普通的dfs也该有的
            {
                vis[i] = true;
                diff_len -= arr[i];
                if (dfs(i + 1, diff_len, sum_len, cur_len))//找下一个,普通的dfs, 剪枝4
                    return true;
                diff_len += arr[i];
                vis[i] = false;
                if (arr[i] == diff_len)//剪枝5,如果走到这里,本次刚好凑成一根棍子,但是既然能走到这,肯定下面的失败了,所以返回上层dfs
                    break;
            }
        }
    }
    return false;
}
bool cmp(const int a, const int b)
{
    return a > b;
}
int main()
{
    while (cin >> n && n)
    {
        Sum = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
            Sum += arr[i];
        }
        sort(arr, arr + n, cmp);//从大到小排序
        memset(vis, false, sizeof(vis));
        int flag = 0;
        for (int i = arr[0]; i <= Sum / 2; i++) //剪枝6,这个为什么是对的,可以举反例,假设要求的i大于Sum/2的话,那么剩下的长度和为Sum-i<i,得出来i+i>Sum,与已知矛盾,所以假设不对。
        {
            if (Sum % i == 0)
            {
                if (dfs(0, i, Sum, i))
                {
                    cout << i << endl;
                    flag = 1;
                    break;
                }
            }
        }
        if (!flag)
            cout << Sum << endl;
    }

    return 0;
} 

时间: 2024-08-08 13:50:48

POJ 1011 sticks 搜索的相关文章

POJ 1011 - Sticks DFS+剪枝

POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    2. 从大到小枚举,因为小长度更灵活, 可拼接可不拼接    3. 因为每一跟木条都要用到, 故若轮到其中一根原始木段选它的第一根木条时,若第一根木条若不满足,则显然第一根木条在接下来任何一根原始木段都不会满足,故无解    4. 由于所有棒子已排序,在DFS时,若某根棒子未被选,则跳过其后面所有与

poj 1011 Sticks 【DFS】+【剪枝】

题意:有未知根(长度一样)木棒(小于等于n),被猪脚任意的截成n段,猪脚(脑抽了)想知道被截之前的最短长度(我推测猪脚得了健忘症). 这道题光理解题意就花了好久,大意就是任意根被截后的木棒拼到一起,能不能组成s(<=n)根的相同的木棒, 例:数据 9  5 1 2 5 1 2 5 1 2 可以组成最短为6 的(5+1, 2+2+2)3根木棒. 策略:深搜. 不过要是传统的深搜的话,TLE妥妥的.所以要剪枝(所谓剪枝,就是多加几个限制条件). 话不多说直接上代码. 代码1: #include <

[POJ 1011]Sticks(DFS剪枝)

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 original

POJ 1011 Sticks 【DFS 剪枝】

题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 154895   Accepted: 37034 Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now

POJ 1011 Sticks(搜索 &amp;&amp; 剪枝 &amp;&amp; 经典)

题意 : 有n根木棍(n<=64),它们由一些相同长度的木棍切割而来,给定这n根木棍的长度,求使得原来长度可能的最小值. 分析 : 很经典的深搜题目,我们发现答案只可能是所有木棍长度总和的因数,那么我们只要去枚举因数然后搜索是否可行即可!具体实现看代码可能更容易看懂,这里不赘述.重要的是体会此类深搜的代码构建过程以及剪枝的考虑的巧妙性! #include<stdio.h> #include<algorithm> #include<string.h> using n

POJ 1011 Sticks dfs,剪枝 难度:2

http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先ans需要满足两个条件1.可以被总集合的和sum整除 2.是总集合的某个子集的和 对于条件1,可以通过试除遍历 对于条件2,可以通过dp预筛选,这两个花费时间都不大 接着搜索能不能划分成集合之和恰为ans的若干集合, 1. 可以从大向小找,因为大的更不灵活,而且所有的元素都需要取到 2.比如对于5,

poj 1011 Sticks ,剪枝神题

木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿来一组等长的木棒.将它们随机地砍断.使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你设计一个程序,帮助乔治计算木棒的可能最小长度.每一节木棍的长度都用大于零的整数表示. Input 输入包括多组数据,每组数据包括两

POJ - 1011 - Sticks (DFS + 剪枝)

题目传送:Sticks 思路:DFS + 剪枝 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include

POJ 1011 Sticks(经典dfs)

Language: Default简体中文 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 120720   Accepted: 27951 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 r