zoukankan      html  css  js  c++  java
  • POJ1011 Sticks

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 43  解决: 8
    [提交] [状态] [讨论版] [命题人:admin]

    题目描述

    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.

    输入

    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.
     

    输出

    The output should contains the smallest possible length of original sticks, one per line.

    样例输入

    9
    5 2 1 5 2 1 5 2 1
    4
    1 2 3 4
    0
    

    样例输出

    6
    5


    搜索剪枝

    #include "bits/stdc++.h"
    
    using namespace std;
    const int maxn = 100;
    
    int s[maxn];
    int vis[maxn];
    
    bool cmp(int a, int b) {
        return a > b;
    }
    
    int num, len;
    int n;
    
    
    bool dfs(int done, int now, int last) {
        if (done >= num) return true;
        if (now == len) return dfs(done + 1, 0, 1);
        int fail = 0;//剪枝1:不重复插入上一个失败的木棍长度
        for (int i = last; i <= n; i++) {
            //剪枝2:构造一根原始木棍时按递减插入,
            // 因为先插入x后插入y和先插入y后插入x是一样的
            if (!vis[i] && now + s[i] <= len && s[i] != fail) {
                vis[i] = 1;
                if (dfs(done, now + s[i], i + 1)) return true;
                vis[i] = 0;
                fail = s[i];
                if (now == 0 || now + s[i] == len) return false;
                //剪枝3,4.如果当前木棍是0或当前木棍以拼成功,却失败了,则说明无解
            }
        }
        return false;
    }
    
    int main() {
        freopen("input.txt", "r", stdin);
        while (scanf("%d", &n)) {
            if (n == 0) break;
            int to = 0;
            int maxx = 0;
            for (int i = 1; i <= n; i++) {
                scanf("%d", &s[i]);
                to += s[i];
                maxx = max(maxx, s[i]);
            }
            sort(s + 1, s + 1 + n, cmp);
            for (int i = maxx; i <= to; i++) {
                if (to % i == 0) {
                    len = i;
                    num = to / i;
                    memset(vis, 0, sizeof(vis));
                    if (dfs(0, 0, 1)) {
                        printf("%d
    ", i);
                        break;
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    安卓环境配置
    [转载]MinGW安装过程
    [转载]解决Android studio新建项目慢的问题
    [转载]图文详解YUV420数据格式
    视频专家之路【四】:ffmpeg简单实战之获取属性
    视频专家之路【三】:Vs开发环境的搭建
    视频专家之路【二】:ffmpeg工具的使用
    视频专家之路【一】:音视频入门知识基础
    二值信号量和互斥锁到底有什么区别?
    【python标准库模块五】Xml模块学习
  • 原文地址:https://www.cnblogs.com/albert-biu/p/10493185.html
Copyright © 2011-2022 走看看