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;
    }
  • 相关阅读:
    后缀数组-另辟蹊径
    Project Euler 不定期更新
    Educational Codeforces Round 93 (Rated for Div. 2)
    Codeforces Round #664 (Div. 2)
    lower_bound和upper_bound的用法
    Codeforces Round #663 (Div. 2)
    Codeforces Round #661 (Div. 3)
    质数笔记
    C++运算符的优先级
    图的构建
  • 原文地址:https://www.cnblogs.com/albert-biu/p/10493185.html
Copyright © 2011-2022 走看看