CF1266E.Spaceship Solitaire

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Bob is playing a game of Spaceship Solitaire. The goal of this game is to build a spaceship. In order to do this, he first needs to accumulate enough resources for the construction. There are nn types of resources, numbered 11 through nn. Bob needs at least aia_i pieces of the ii-th resource to build the spaceship. The number aia_i is called the goal for resource ii.

Each resource takes 11 turn to produce and in each turn only one resource can be produced. However, there are certain milestones that speed up production. Every milestone is a triple (sj,tj,uj)(s_j, t_j, u_j), meaning that as soon as Bob has tjt_j units of the resource sjs_j, he receives one unit of the resource uju_j for free, without him needing to spend a turn. It is possible that getting this free resource allows Bob to claim reward for another milestone. This way, he can obtain a large number of resources in a single turn.

The game is constructed in such a way that there are never two milestones that have the same sjs_j and tjt_j, that is, the award for reaching tjt_j units of resource sjs_j is at most one additional resource.

A bonus is never awarded for 00 of any resource, neither for reaching the goal aia_i nor for going past the goal — formally, for every milestone 0<tj<asj0 \lt t_j \lt a_{s_j}.

A bonus for reaching certain amount of a resource can be the resource itself, that is, sj=ujs_j = u_j.

Initially there are no milestones. You are to process qq updates, each of which adds, removes or modifies a milestone. After every update, output the minimum number of turns needed to finish the game, that is, to accumulate at least aia_i of ii-th resource for each i∈[1,n]i \in [1, n].

鲍勃正在玩一款名为“太空飞船纸牌”的游戏。这款游戏的目标是建造一艘太空飞船。为此,他首先需要积攒足够的资源用于建造。共有 nn 种资源,编号为 11 到 nn。鲍勃至少需要 aia_i 个第 ii 种资源才能建造飞船。数值 aia_i 被称为第 ii 种资源的“目标值”。

每种资源的生产需耗时 1 回合,且每回合只能生产一种资源。然而,游戏中存在若干“里程碑”,可加速资源生产。每个里程碑是一个三元组 (sj,tj,uj)(s_j, t_j, u_j),其含义为:一旦鲍勃拥有了 tjt_j 个第 sjs_j 种资源,他将免费获得 1 个第 uju_j 种资源,而无需消耗回合。获得该免费资源可能又满足另一个里程碑的触发条件,从而在单回合内连锁获取大量资源。

游戏规则保证:不存在两个具有相同 sjs_j 和 tjt_j 的里程碑,即对任意资源 sjs_j 达到数量 tjt_j 所触发的奖励至多只有一种额外资源。

任何资源的数量为 00 时不会触发奖励;达到或超过其目标值 aia_i 时也不会触发奖励——形式化地说,对每个里程碑均有 0<tj<asj0 \lt t_j \lt a_{s_j}。

某个资源达到特定数量所触发的奖励,可以是该资源自身,即允许 sj=ujs_j = u_j。

初始时不存在任何里程碑。你需要处理 qq 次更新操作,每次更新会添加、删除或修改一个里程碑。每次更新后,请输出完成游戏所需的最少回合数,即对每个 i∈[1,n]i \in [1, n],均至少拥有 aia_i 个第 ii 种资源所需的最小回合数。

输入格式

The first line contains a single integer nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) — the number of types of resources.

The second line contains nn space separated integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \leq a_i \leq 10^9), the ii-th of which is the goal for the ii-th resource.

The third line contains a single integer qq (1≤q≤1051 \leq q \leq 10^5) — the number of updates to the game milestones.

Then qq lines follow, the jj-th of which contains three space separated integers sjs_j, tjt_j, uju_j (1≤sj≤n1 \leq s_j \leq n, 1≤tj<asj1 \leq t_j \lt a_{s_j}, 0≤uj≤n0 \leq u_j \leq n). For each triple, perform the following actions:

  • First, if there is already a milestone for obtaining tjt_j units of resource sjs_j, it is removed.
  • If uj=0u_j = 0, no new milestone is added.
  • If uj≠0u_j \neq 0, add the following milestone: "For reaching tjt_j units of resource sjs_j, gain one free piece of uju_j."
  • Output the minimum number of turns needed to win the game.

第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5)—— 资源类型的数量。

第二行包含 nn 个用空格分隔的整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \leq a_i \leq 10^9),其中第 ii 个数表示第 ii 种资源的目标数量。

第三行包含一个整数 qq(1≤q≤1051 \leq q \leq 10^5)—— 游戏里程碑更新操作的次数。

接下来是 qq 行,其中第 jj 行包含三个用空格分隔的整数 sjs_j、tjt_j、uju_j(1≤sj≤n1 \leq s_j \leq n,1≤tj<asj1 \leq t_j \lt a_{s_j},0≤uj≤n0 \leq u_j \leq n)。对每个三元组,执行以下操作:

  • 首先,若已存在一个“获取资源 sjs_j 的 tjt_j 单位”的里程碑,则将其移除。
  • 若 uj=0u_j = 0,则不添加新的里程碑。
  • 若 uj≠0u_j \neq 0,则添加如下里程碑:“当获取资源 sjs_j 的 tjt_j 单位时,获得一个免费的 uju_j 类物品。”
  • 输出赢得游戏所需的最少回合数。

输出格式

Output qq lines, each consisting of a single integer, the ii-th represents the answer after the ii-th update.

输出 qq 行,每行一个整数,其中第 ii 行表示第 ii 次更新后的答案。

输入输出样例

  • 输入#1

    2
    2 3
    5
    2 1 1
    2 2 1
    1 1 1
    2 1 2
    2 2 0

    输出#1

    4
    3
    3
    2
    3

说明/提示

After the first update, the optimal strategy is as follows. First produce 22 once, which gives a free resource 11. Then, produce 22 twice and 11 once, for a total of four turns.

After the second update, the optimal strategy is to produce 22 three times — the first two times a single unit of resource 11 is also granted.

After the third update, the game is won as follows.

  • First produce 22 once. This gives a free unit of 11. This gives additional bonus of resource 11. After the first turn, the number of resources is thus [2,1][2, 1].
  • Next, produce resource 22 again, which gives another unit of 11.
  • After this, produce one more unit of 22.

The final count of resources is [3,3][3, 3], and three turns are needed to reach this situation. Notice that we have more of resource 11 than its goal, which is of no use.

第一次更新后,最优策略如下:首先生产一次 22,获得一个免费的资源 11;然后生产两次 22 和一次 11,共需四回合。

第二次更新后,最优策略是连续三次生产 22——其中前两次生产还会额外各授予一个单位的资源 11。

第三次更新后,游戏获胜过程如下:

  • 首先生产一次 22,获得一个免费的资源 11,并由此触发资源 11 的额外奖励。第一回合结束后,资源数量变为 [2,1][2, 1]。
  • 接着再次生产资源 22,再获得一个单位的 11。
  • 此后,再生产一个单位的 22。

最终资源数量为 [3,3][3, 3],达成该状态共需三回合。注意,此时资源 11 的数量已超过其目标值,超出部分无任何作用。

输入解题思路,AI测评打分。不知道怎么写?

首页