QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#4304. 坚固背包问题

统计

背包问题是计算机科学中的一个经典问题。

其描述如下:现有 $n$ 个物品,每个物品都有已知的重量 $w_i$ 和价值 $cost_i$。同时,你已知背包的容量 $W$——即所选物品总重量的上限。任务是选择若干物品,使其总重量不超过 $W$,且总价值尽可能大。

在本题中,你不需要解决经典的背包问题。评测组已经解决了该问题并找到了确切答案:$x$ 是容量为 $W$ 的背包所能装下的物品的最大总价值。评测组不会告诉你这个值。

你的任务是解决“公司背包问题”(The Firm Knapsack Problem)。现在,标称容量为 $W$ 的背包实际上可以容纳重量不超过 $\frac{3}{2}W$ 的物品。你需要解决这个放宽约束后的问题,且效果不能比评测组在原始约束 $W$ 下解决的结果差。

换句话说,你需要找到一组物品,其总价值至少为 $x$,且总重量不超过 $\frac{3}{2}W$。

你需要解决 $t$ 组测试数据。

输入格式

第一行包含测试数据的组数。接下来是各组测试数据,格式如下:

每组测试数据的第一行包含两个整数 $n$ 和 $W$ ($1 \le n \le 10^5$; $1 \le W \le 10^{12}$),分别表示物品数量和背包的标称容量。

接下来的 $n$ 行描述物品。每行包含两个整数 $w_i$ 和 $cost_i$ ($1 \le w_i, cost_i \le 10^6$),分别表示物品的重量和价值。

所有测试数据的 $n$ 之和不超过 $10^5$。

输出格式

对于每组测试数据,输出在重量约束 $\frac{3}{2}W$ 下选择的物品集合,格式如下:

第一行包含所选物品的数量。

第二行包含所选物品的编号 $i_1, i_2, \dots, i_k$ ($1 \le i_j \le n$)。所有编号 $i_j$ 必须互不相同。物品按输入顺序从 $1$ 到 $n$ 编号。

如果存在多种方案,输出其中任意一种即可。

样例

输入 1

3
3 10
5 100
5 100
4 99
3 100
97 100
98 101
99 90
3 100
55 100
99 150
200 200

输出 1

3
3 1 2
1
2
1
2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.