zoukankan      html  css  js  c++  java
  • hdu Sticks

    Sticks

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 206    Accepted Submission(s): 67
     
    Problem 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 file 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
    ACM暑期集训队练习赛(七)
     
    Recommend
    lcy

    分析:去年做完全没思路,今天增加一个关键剪枝后0msAC。

    此题关键:逆序快排一次,因为长度与灵活度成反比。

    关键剪枝:每次构造新木棍时,未用的最大的小木棍必须使用。

    其他剪枝:在构造某根小木棒时,相同的长度未用的只搜一次。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int st[70];
    int vis[70];
    int len, tot;
    int n;
    
    bool cmp(int a, int b) {
        return a>b;
    }
    
    int dfs(int now, int num, int pos) {
        if (num == tot)
            return 1;
        int i, t;
        if (now == 0) {
            while (vis[pos])
                ++pos;
            vis[pos] = 1;
            if (st[pos] == len) {
                if (dfs(0, num + 1, 0))
                    return 1;
                else {
                    vis[pos] = 0;
                    return 0;
                }
            } else {
                if (dfs(st[pos], num, pos + 1))
                    return 1;
                else {
                    vis[pos] = 0;
                    return 0;
                }
            }
        }
        for (i = pos; i < n; ++i) {
            if (i > 0 && !vis[i - 1] && st[i] == st[i - 1])
                continue;
            if (!vis[i]) {
                t = now + st[i];
                if (t == len) {
                    vis[i] = 1;
                    if (dfs(0, num + 1, 0))
                        return 1;
                    else {
                        vis[i] = 0;
                        return 0;
                    }
                } else if (t < len) {
                    vis[i] = 1;
                    if (dfs(t, num, i + 1))
                        return 1;
                    vis[i] = 0;
                }
            }
        }
        return 0;
    
    }
    
    int main() {
        int i, sum;
        while (scanf("%d", &n) != EOF && n) {
            sum = 0;
            for (i = 0; i < n; ++i) {
                scanf("%d", &st[i]);
                sum += st[i];
            }
            sort(st, st + n, cmp);
            for (len = st[0]; len < sum; ++len) {
                if (sum % len == 0) {
                    memset(vis, 0, sizeof (vis));
                    tot = sum / len;
                    if (dfs(0, 0, 0)) {
                        break;
                    }
                }
            }
            printf("%d\n", len);
        }
        return 0;
    }
  • 相关阅读:
    NETCore下IConfiguration和IOptions的用法
    关于将vs项目推到GitHub上遇到的问题
    SQL GROUP BY对多个字段进行分组
    sql if else 语句
    CSS
    CSS
    JS
    22 ~ express ~ 内容评论实现
    css ~ a标签占满父级元素
    21 ~ express ~ 内容详情展示 和 阅读数处理
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2666257.html
Copyright © 2011-2022 走看看