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;
    }
  • 相关阅读:
    #sort 快速排序 20. 9.14
    #Trie Trie树调试模板 20.09.21
    #operator ——“Kruskal算法求最小生成树 中的 operator” ~20.8.17
    #STL #List 容器函数以及一些内置函数的用法
    刷题周记(三)——#最小生成树:Kruskal#二分图:染色法、匈牙利算法#拓扑#DFS:排列数字、n-皇后#BFS:走迷宫、八格码#List容器
    #周测 9 —— 高低位交换、Subsequences Summing to Sevens S、积木大赛、跳石头
    刷题周记(二)——KMP,Trie,最大异或对,(并查集)合并集合、连通块中点的数量、食物链,堆排序,单多源最短路、Dijkstra、bellman-ford、SPFA、Floyd、(堆优化)Prim
    4.SQL(数据库变更)
    3.SQL(查询)
    2.Oracle基本使用
  • 原文地址:https://www.cnblogs.com/DukeLv/p/9708935.html
Copyright © 2011-2022 走看看