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

Posted wsmrxc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper相关的知识,希望对你有一定的参考价值。

用pair写dp, 有种新鲜的感觉.
就是状压dp, pair第一维记录用了几个电梯, 第二维记录还剩多少空间, 取min转移即可.
用pair主要是为了使代码简洁.

#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int MAXN = 18 + 1;
inline int read(){
    char ch = getchar(); int x = 0;
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return x;
}

int N, W;
int c[MAXN];
P f[(1 << 18)];

int main(){
    cin>>N>>W;
    for(int i = 0; i < N; i++) c[i] = read();
    memset(f, 0x3f, sizeof(f)), f[0] = P(0, 0);
    for(int i = 0; i < (1 << N); i++) {
        for(int j = 0; j < N; j++) if(!(i & (1 << j)))
            f[i | (1 << j)] = min(f[i | (1 << j)], 
            f[i].second + c[j] <= W ? P(f[i].first, f[i].second + c[j]) : P(f[i].first + 1, c[j]));
    }
    printf("%d
", f[(1 << N) - 1].first + (f[(1 << N) - 1].second != 0));
    return 0;
}

以上是关于P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper的主要内容,如果未能解决你的问题,请参考以下文章

P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper [模拟退火]

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

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

题解Luogu P3052 USACO12摩天大楼里的奶牛Cows in a Skyscraper

[luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)

bzoj 1597: [Usaco2008 Mar]土地购买 2011-12-27