• P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp


    这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大.

    再单开一个g数组,存剩余空间就行了.

    题干:

    题目描述
    
    A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.
    
    The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.
    
    给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)
    输入输出格式
    输入格式:
    
    * Line 1: N and W separated by a space.
    
    * Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.
    
    输出格式:
    
    * A single integer, R, indicating the minimum number of elevator rides needed.
    
    one of the R trips down the elevator.
    
    输入输出样例
    输入样例#1: 复制
    
    4 10 
    5 
    6 
    3 
    7 
    
    输出样例#1: 复制
    
    3 
    
    说明
    
    There are four cows weighing 5, 6, 3, and 7 pounds. The elevator has a maximum weight capacity of 10 pounds.
    
    We can put the cow weighing 3 on the same elevator as any other cow but the other three cows are too heavy to be combined. For the solution above, elevator ride 1 involves cow #1 and #3, elevator ride 2 involves cow #2, and elevator ride 3 involves cow #4. Several other solutions are possible for this input.

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define duke(i,a,n) for(int i = a;i <= n;i++)
    #define lv(i,a,n) for(int i = a;i >= n;i--)
    #define clean(a) memset(a,0,sizeof(a))
    const int INF = 1 << 30;
    typedef long long ll;
    typedef double db;
    template <class T>
    void read(T &x)
    {
        char c;
        bool op = 0;
        while(c = getchar(), c < '0' || c > '9')
            if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(), c >= '0' && c <= '9')
            x = x * 10 + c - '0';
        if(op) x = -x;
    }
    template <class T>
    void write(T x)
    {
        if(x < 0) putchar('-'), x = -x;
        if(x >= 10) write(x / 10);
        putchar('0' + x % 10);
    }
    int n,W;
    int w[25];
    int g[1 << 19],f[1 << 19];
    int main()
    {
        read(n);read(W);
        duke(i,1,n)
        {
            read(w[i]);
        }
        memset(f,127,sizeof(f));
        f[0] = 1;
        g[0] = W;
        duke(i,0,(1 << n) - 1)
        {
            duke(j,1,n)
            {
                if(i & (1 << (j - 1)))
                    continue;
                if(g[i] >= w[j] && f[i | (1 << (j - 1))] >= f[i])
                {
                    f[i | (1 << (j - 1))] = f[i];
                    g[i | (1 << (j - 1))] = max(g[i | (1 << (j - 1))],g[i] - w[j]);
                }
                else if(g[i] < w[j] && f[i | (1 << (j - 1))] >= f[i] + 1)
                {
                    f[i | (1 << (j - 1))] = f[i] + 1;
                    g[i | (1 << (j - 1))] = max(g[i | (1 << (j - 1))],W - w[j]);
                }
            }
        }
        printf("%d
    ",f[(1 << n) - 1]);
        return 0;
    }
  • 相关阅读:
    koa 放置 前台打包dist 目录
    tomcat startup.bat 包含springboot的输出 里面乱码的解决方案
    base64 转文件上传
    4时4态 加被动 例句:I will have been being done
    软件推荐 Notable / 现改用 Vnote 了
    [win10] 开始-设置 / 右键-显示设置 / 右键个性化 等都不好使了。。 ms-settings:display
    viewui tree 自定义化(源码copy出来改动)#添加 获取selected 解决方案
    idea 暂存 Stash Changes Git/Repository/Stash Changes 恢复暂存 UnStash Changes
    vm 虚拟机总是蓝屏 移除打印机和声卡 移除这俩硬件 (大文件用飞秋传输)
    docker中mysql 汉字乱码,显示问号
  • 原文地址:https://www.cnblogs.com/DukeLv/p/9708935.html
Copyright © 2020-2023  润新知