标签:inline rap 代码简洁 ring 代码 cond ems sizeof ace
用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\n", f[(1 << N) - 1].first + (f[(1 << N) - 1].second != 0));
return 0;
}
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
标签:inline rap 代码简洁 ring 代码 cond ems sizeof ace
原文地址:https://www.cnblogs.com/wsmrxc/p/9818557.html