Loading

P3052 [USACO12MAR]Cows in a Skyscraper G 状压DP,随机化,玄学

P3052 [USACO12MAR]Cows in a Skyscraper G 状压DP,随机化,玄学

题意

给出\(n\)个物品,体积为\(w_i\),现把其分成若干组,要求总体积\(\leq W\),问最小的组数

分析

可以状压dp,但是数据好像比较随意

就硬随机呗 ,注意小技巧,微扰W

代码

ll w[20];

int main() {
    int res = 25;
    ll n = readint(), W = readll();
    for (int i = 0; i < n; i++)
        w[i] = readll();
    W *= 1.005;
    int times = 1500000;
    while (times--) {
        random_shuffle(w, w + n);
        int cnt = 0;
        ll tmp = 0;
        for (int i = 0; i < n; i++) {
            if (tmp + w[i] > W) cnt++, tmp = w[i];
            else tmp += w[i];
        }
        if (tmp) cnt++;
        res = min(res, cnt);;
    }
    printf("%d", res);
}
posted @ 2020-09-10 19:51  MQFLLY  阅读(136)  评论(0编辑  收藏  举报