BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper

时间:2023-03-08 16:28:57
BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper

首先比较容易想到是状态压缩DP

令$f[S]$表示选取了集合$S$以后,已经送了最少次数$cnt$且当前电梯剩下的体积$rest$最大(即$f[S]$是一个二元组$(cnt, rest)$)

于是$f[S] = min_{i \in S} f[S - {i}] + v[i]$

重载的$<$和$+$运算详情就请看程序好了,反正就是一个贪心思想,总复杂度$O(n * 2 ^ {n - 1})$

 /**************************************************************
Problem: 2621
User: rausen
Language: C++
Result: Accepted
Time:532 ms
Memory:13092 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
const int N = ;
const int S = << N;
const int inf = 1e9; int n, mx, mxs;
int a[S]; struct data {
int cnt, rest;
data(int _c = , int _r = mx) : cnt(_c), rest(_r) {} inline data operator + (int t) const {
static data res;
res = *this;
res.rest -= t;
if (res.rest < ) ++res.cnt, res.rest += mx;
return res;
} inline bool operator < (const data &d) const {
return cnt == d.cnt ? rest < d.rest : cnt < d.cnt;
}
} f[S]; int main() {
int i, s, t, now;
scanf("%d%d", &n, &mx);
mxs = ( << n) - ;
for (i = ; i <= n; ++i) scanf("%d", &a[ << i - ]);
f[] = data(, mx);
for (s = ; s <= mxs; ++s) {
t = s, f[s] = data(inf, mx);
while (t) {
now = t & (-t);
f[s] = min(f[s], f[s ^ now] + a[now]);
t -= now;
}
}
printf("%d\n", f[mxs].cnt + (f[mxs].rest != mx));
return ;
}