QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 256 MB مجموع النقاط: 100

#4585. 贪心背包问题

الإحصائيات

Budi 在大学课堂上接触到了经典的 0-1 背包问题。现有 $N$ 个物品,编号从 $1$ 到 $N$。第 $i$ 个物品具有正权重 $W_i$ 和正价值 $V_i$。目标是选取零个或多个物品,使得总权重不超过 $M$,且总价值最大。

距离 Budi 上次研究这个问题已经过去很久了,他已经记不清该如何求解了。于是,Budi 设计了一个贪心算法来尝试解决这个问题。算法逻辑如下:按顺序从第 $1$ 个物品处理到第 $N$ 个物品。如果第 $i$ 个物品在加入后总权重不超过 $M$,则选取该物品;否则,忽略它。输出所有被选取物品的总价值。

该贪心算法的伪代码如下:

GreedyKnapsack(W[1..N], V[1..N], M):
    total_value = 0
    total_weight = 0
    for i = 1 to N:
        if total_weight + W[i] <= M:
            total_weight = total_weight + W[i]
            total_value = total_value + V[i]
    return total_value

当然,Budi 意识到这样的贪心算法可能无法得到最优解,但他目前并不在意。Budi 注意到该贪心算法的输出对 $M$ 很敏感。因此,他决定在 $M$ 从 $1$ 到给定的上限 $T$ 的范围内运行该贪心算法,并确定他能得到的最大输出值。

本题的任务是帮助 Budi 确定通过改变 $M$($1 \le M \le T$)所能得到的贪心算法的最大输出值。

例如,设 $N = 5, T = 10, W_{1..5} = \{10, 1, 2, 3, 4\}$,$V_{1..5} = \{1, 1, 1, 1, 1\}$。在此例中,能得到的最大输出为 $3$(例如当 $M = 6$ 时;贪心算法将选取第 $2, 3, 4$ 个物品,总权重为 $1 + 2 + 3 = 6$,总价值为 $1 + 1 + 1 = 3$)。注意,如果设 $M = 10$,贪心算法将仅选取第 $1$ 个物品,总权重为 $10$,总价值为 $1$。

输入格式

输入第一行包含两个整数 $N$ 和 $T$ ($1 \le N \le 100\,000; 1 \le T \le 10^{10}$),分别表示物品数量和上限。第二行包含 $N$ 个整数 $W_i$ ($1 \le W_i \le 100\,000$),表示第 $i$ 个物品的权重。第三行包含 $N$ 个整数 $V_i$ ($1 \le V_i \le 100\,000$),表示第 $i$ 个物品的价值。

输出格式

输出一行,包含一个整数,表示通过上述贪心算法所能得到的最大输出值。

样例

样例输入 1

5 10
10 1 2 3 4
1 1 1 1 1

样例输出 1

3

说明 1

这是题目描述中的示例。

样例输入 2

5 1000000000
10 1 2 3 4
30 2 15 7 11

样例输出 2

65

说明 2

你可以将 $M$ 设置得足够大,使得所有物品都能被选取,即 $M \ge 20$。

样例输入 3

5 20
4 9 5 1 3
203 175 131 218 304

样例输出 3

900

说明 3

当 $M = 17$ 时可获得最大输出。此时会选取第 $1, 2, 4, 5$ 个物品,总权重为 $4 + 9 + 1 + 3 = 17$,总价值为 $203 + 175 + 218 + 304 = 900$。

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.