zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    go语言基础学习
    VBA汇总同目录下的所有工作簿数据到另一个工作簿,并进行统计
    彻底关闭win10后台同步数据(转自技术社区)
    在WIN10上安装ESXI-Comstomer (转自技术社区)
    squid代理允许FTP访问设置
    Powershell 脚本判断制定路径下文件是否存在(来源于网络-转载)
    Python集合(set)类型的操作 (转)
    python3.5.2中文字符乱码问题解决
    Debian 中文环境设置
    Python 列表推导实例
  • 原文地址:https://www.cnblogs.com/DukeLv/p/9708935.html
Copyright © 2011-2022 走看看