其他分享
首页 > 其他分享> > 【Gym 102893 L】The Firm Knapsack Problem (贪心)

【Gym 102893 L】The Firm Knapsack Problem (贪心)

作者:互联网

题目链接

题目大意

一个 01 背包问题,物品数 n ≤ 1 0 5 n\le 10^5 n≤105 ,容量 W ≤ 1 0 12 W\le 10^{12} W≤1012 。将体积上限放宽到 3 2 W \frac{3}{2}W 23​W ,求一组解使得物品总价值不低于容量为 W W W 时的最优解。

思路

01 背包问题通常是 DP 求解,但该数据范围显然不可行。

先考虑将物品按(价值/体积)进行排序,然后贪心取,取到放不下为止。这样取到的不一定是满足要求。但显然,贪心方案一定是当前总体积的最优解。(最优解的总体积可能比它大,总(价值/体积)一定不会比它高。)

因此,如果所有物品体积都不超过 W 2 \frac{W}{2} 2W​ ,那么贪心方案一定满足要求。

否则,可以发现,方案中体积超过 W 2 \frac{W}{2} 2W​ 的物品一定不超过 1 个。

不妨考虑枚举大物品。如果大物品和 W W W 容量最优解的相同,那么再对小物品贪心,得到的方案也一定满足要求。

所以考虑用 two-pointers 求解

代码

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;
typedef long long ll;
const int N = 100005;
int T;
int n;
ll W;
class pack {
   public:
    int id;
    ll w, cost;
    pack() {}
    pack(int _id, ll _w, ll _cost) : id(_id), w(_w), cost(_cost) {}
    void read() { scanf("%lld%lld", &w, &cost); }
    bool operator<(const pack &rhs) const {
        return cost * rhs.w > w * rhs.cost;
    }
} small[N], big[N];

int n_small, n_big;
int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%d%lld", &n, &W);
        n_small = n_big = 0;
        rep(i, 1, n) {
            ll w, cost;
            scanf("%lld%lld", &w, &cost);
            if (w * 2 > W)
                big[++n_big] = pack(i, w, cost);
            else
                small[++n_small] = pack(i, w, cost);
        }
        sort(big + 1, big + n_big + 1,
             [](pack lhs, pack rhs) { return lhs.w > rhs.w; });
        big[n_big + 1] = pack(n + 1, 0, 0);
        sort(small + 1, small + n_small + 1);
        // printf("%d %d\n", n_big, n_small);
        int top = 0;
        ll small_w = 0, small_cost = 0;
        ll ans = 0;
        int ret_big = n_big + 1, ret_top = 0;
        rep(i, 1, n_big + 1) {
            if (big[i].w > W * 3 / 2) continue;
            while (top < n_small &&
                   big[i].w + small_w + small[top + 1].w <= W * 3 / 2) {
                ++top;
                small_w += small[top].w;
                small_cost += small[top].cost;
            }
            // printf("%d ", top);
            if (big[i].cost + small_cost > ans) {
                ans = big[i].cost + small_cost;
                ret_big = i;
                ret_top = top;
            }
        }
        printf("%d\n", ret_top + (ret_big <= n_big));
        rep(i, 1, ret_top) printf("%d ", small[i].id);
        if (ret_big <= n_big) printf("%d ", big[ret_big].id);
        printf("\n");
    }
    return 0;
}

标签:Firm,cost,big,Gym,ret,102893,small,top,pack
来源: https://blog.csdn.net/qq_47903865/article/details/120962431