搜索 + 剪枝 --- POJ 1101 : Sticks

Sticks

Problem‘s Link:   http://poj.org/problem?id=1011



Mean:

http://poj.org/problem?id=1011&lang=zh-CN&change=true

analyse:

爆搜,但是其中蕴含着很多剪枝。

Time complexity: O(n^2)

Source code: 

//  Memory   Time
//  1347K     0MS
//   by : Snarl_jsb
//   2014-11-07-17.14
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std;

int n,sum,len;
vector<int> sti(65);
vector<bool> used(sti.size());

// k-----从第k根开始往后判断
// total-----该回合还剩下的长度
// sum----未选取的棍子的总长度
bool dfs(int k,int total,int sum)
{
    if(total==0)
    {
        sum-=len;
        if(sum==0)
        {
            return true;
        }
        else
        {
            total=len;
            for(k=0;used[k];++k); //找出未选的最靠前的一根
            used[k]=1;
            //由于从第k根开始选,第k根必选,从k+1根开始搜索
            if(dfs(k+1,total-sti[k],sum))
                return true;
            used[k]=0;
            sum+=len;
        }
    }
    else
    {
        for(int i=k;i<n;++i)
        {
            if(sti[i]==sti[i-1]&&(!used[i-1])&&i>0)
                continue;
            if(total>=sti[i]&&(!used[i]))
            {
                total-=sti[i];
                used[i]=1;
                if(dfs(i,total,sum))
                    return true;
                total+=sti[i];
                used[i]=0;
                if(sti[i]==total)
                break;
            }
        }
    }
    return false;
}

bool cmp(int a,int b)
{
    return a>b;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
//    freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
//    freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
    while(cin>>n,n)
    {
        sum=0;
        int tmp;
        sti.clear();
        for(int i=0;i<n;++i)
        {
            used[i]=0;
            cin>>tmp;
            sum+=tmp;
            sti.push_back(tmp);
        }
        sort(sti.begin(),sti.end(),cmp);
        bool flag=false;
        for(len=sti.front();len<=sum/2;++len)
        {
            if(sum%len==0)
            {
                if(dfs(0,len,sum))
                {
                    cout<<len<<endl;
                    flag=1;
                    break;
                }
            }
        }
        if(!flag)
            cout<<sum<<endl;
    }
    return 0;
}
/*
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
8
45 56 78 456 1231 456456 45 123
*/

  

时间: 2024-08-02 19:11:59

搜索 + 剪枝 --- POJ 1101 : Sticks的相关文章

搜索+剪枝 POJ 1416 Shredding Company

POJ 1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5231   Accepted: 2964 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would

poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一个边界外,跳到了另一边的边界外,每跳一次对那个点进行标记. 现在给你很多青蛙跳过后的所标记的所有点,那请你从这些点里面找出 一条可能的路径里面出现过的标记点最多. 分析:先排序(目的是方便剪枝,break),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

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 2531 搜索剪枝

Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description A university network is composed of N computers. System administrators gathered informat

[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+剪枝

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

【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move

poj  1568:Find the Winning Move   [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cases, followed by a line beginning with a dollar sign that signals the end of the file. Each test case begins with a line containing a question mark and

【搜索剪枝】HDU 5469 Antonidas

通道 题意:给出1字母树,询问一字符串是否出现在该树中 思路:直接搜索剪枝,有人点分治?写了几发都T了..有人会了教我? 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Edge { int v, nxt; Edge () { } Edge (int _v, int _n) { v = _v, nxt = _n; } }; const in

USACO/fence8 迭代加深搜索+剪枝

题目链接 迭代加深搜索思想. 枚举答案K,考虑到能否切出K个木头,那么我们当然选最小的K个来切. 1.对于原材料,我们是首选最大的还是最小的?显然,首选大的能够更容易切出,也更容易得到答案. 2.对于目标木头,我们是优先得到最大的还是最小的?显然,由于K个木头我们都要得到,那么当然先把最大的(最难得到的)先得到,这种搜索策略更优. 3.假设总原材料为all,前K个木头总和为sum,那么all-sum就是这一次切割过程中能[浪费]的最大数目.对于一个切剩下的原材料,若它比最小的目标木头还要小,则它