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

题目链接

题目大意

一个 01 背包问题,物品数 n ≤ 1 0 5 n\le 10^5 n105 ,容量 W ≤ 1 0 12 W\le 10^{12} W1012 。将体积上限放宽到 3 2 W \frac{3}{2}W 23W ,求一组解使得物品总价值不低于容量为 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值