QOJ.ac

QOJ

时间限制: 2 s 内存限制: 1024 MB 总分: 100

#4362. 均衡饮食

统计

每天,Danny 都会从糖果店买一颗糖果并吃掉。商店里有 $m$ 种糖果,编号从 $1$ 到 $m$。Danny 知道均衡饮食很重要,并将这一概念应用到他的糖果购买中。对于每种糖果 $i$,他都设定了一个目标比例,即一个实数 $f_i$ ($0 < f_i \le 1$)。他希望他所吃过的所有糖果中,类型为 $i$ 的糖果所占的比例大致等于 $f_i$。更准确地说,设 $s_i$ 为 Danny 已经吃过的类型为 $i$ 的糖果数量,设 $n = \sum_{i=1}^m s_i$。如果对于每一个 $i$,都有 $$n f_i - 1 < s_i < n f_i + 1$$ 则称这组糖果是均衡的。

Danny 已经购买并食用了一段时间的糖果,在此期间,他所吃的糖果组合始终保持均衡。他现在想知道,在继续满足此条件的前提下,他还能再买多少颗糖果。给定目标比例 $f_i$ 以及他目前已食用的糖果序列,请确定他还能再买并吃掉多少颗糖果,使得在任何时刻糖果组合都是均衡的。

输入格式

输入包含三行。第一行包含两个整数 $m$ ($1 \le m \le 10^5$),表示糖果的种类数;$k$ ($0 \le k \le 10^5$),表示 Danny 已经吃过的糖果数量。

第二行包含 $m$ 个正整数 $a_1, \dots, a_m$。这些数字与 $f_1, \dots, f_m$ 成正比,即 $f_i = \frac{a_i}{\sum_{j=1}^m a_j}$。保证所有 $a_j$ 的总和不超过 $10^5$。

第三行包含 $k$ 个整数 $b_1, \dots, b_k$ ($1 \le b_i \le m$),其中每个 $b_i$ 表示 Danny 在第 $i$ 天购买并食用的糖果类型。保证该序列的每一个前缀(包括整个序列)都是均衡的。

输出格式

输出 Danny 在保持饮食持续均衡的前提下,最多还能再购买并食用的糖果数量。如果没有上限,则输出 forever

样例

样例输入 1

6 5
2 1 6 3 5 3
1 2 5 3 5

样例输出 1

1

样例输入 2

6 4
2 1 6 3 5 3
1 2 5 3

样例输出 2

forever

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.