poj 1011 sticks

4 篇文章 0 订阅
3 篇文章 0 订阅

poj 1011 sticks

Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 160439 Accepted: 38508
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
非常经典的一道深搜题,这题要注意的是剪枝优化,否则就会TLE o(╥﹏╥)o 。
大致思路:将木棒从大到小排序,合成的木棒最小长度只可能大于等于当前木棒中最大的长度,而且必须能够整除所有木棒长度之和。然后从小到大枚举目标长度,利用dfs判断该目标长度是否为可行解,若可行,则此解即为目标长度最小值,否则继续枚举下一个长度。当然这里有一些剪枝,没有剪枝的话,就会超时。

代码中有详细解释

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

int n, sum, sticks[70]; //n为木棒被分块的数量,sum为木棒所有的长度,sticks为每块木棒的长度
bool vis[70];    //木棒是否被选中
bool have; //1表示有解
int maxx;   //木棒最大长度
bool cmp(int a, int b)//从大到小排序
{
    return a > b;
}
//合成目标长度剩余长度
//num选择木棒的数量
//pos当前所选木棒
void dfs(int res, int num, int pos)
{
    //表示找到解,返回
    if(have) return;

    //所有木棒已被选,have置1,返回
    //当所有木棒都被选了,因为木棒长度可以整除总长度,则此时res一定为零,故找到了解
    if(num == n) {
        have = 1;
        return;
    }
    //当前合成了一根目标长度木棒,但木棒并没有全部使用,故继续往后下搜索
    if(res== 0 && num < n) dfs(maxx, num, 0);
    //搜索木棒
    for(int i = pos; i < n; ++i)
    {
        //当前木棒未被使用且小于等于合成目标长度所需剩余长度
        if(!vis[i] && sticks[i] <= res)
        {
            //标记当前木棒已用来合成
            vis[i] = 1;
            //继续往下搜索
            dfs(res - sticks[i], num + 1, i+1);
            vis[i] = 0;  //回溯
            //剪枝,当合成目标剩余长度等于当前木棒,当前木棒不符合,则后面的木棒也就不符合,则后面的不用继续搜,
            if(res == sticks[i]) return;
            //剩余长度等于目标长度,则后面所有的长度就组成了目标长度,不用继续搜索了
            if(res == maxx) return;
            //后面长度相同的木棒都不用搜索
            while(sticks[i] == sticks[i + 1]) i++;
        }
    }
}
int main()
{

    while(cin >> n)
    {
        if(!n) break;
        sum = 0;
        for(int i = 0; i < n; ++i)
        {
            cin >> sticks[i];
            sum += sticks[i];
        }
        sort(sticks, sticks + n, cmp);
        maxx = sticks[0];
        if(maxx > sum - sticks[0]) //当木棒长度大于剩余长度时,则原来最小长度即为所有木棒长度和
        {
            cout << sum << endl;
            continue;
        }
        have = false;
        while(!have)
        {
            //当总长度不能被最大木棒长度整除时,则其他木棒必然不能合成相同长度木棒,故须剪枝
            while(sum % maxx != 0) maxx++;
            //初始化vis
            mem(vis, false);
            dfs(maxx, 0, 0);
            if(have) break;
            maxx++;
        }
        cout  << maxx << endl;
    }
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值