CF1784C.Monsters (hard version)

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. In this version, you need to find the answer for every prefix of the monster array.

In a computer game, you are fighting against nn monsters. Monster number ii has aia_i health points, all aia_i are integers. A monster is alive while it has at least 11 health point.

You can cast spells of two types:

  1. Deal 11 damage to any single alive monster of your choice.
  2. Deal 11 damage to all alive monsters. If at least one monster dies (ends up with 00 health points) as a result of this action, then repeat it (and keep repeating while at least one monster dies every time).

Dealing 11 damage to a monster reduces its health by 11.

Spells of type 1 can be cast any number of times, while a spell of type 2 can be cast at most once during the game.

For every k=1,2,…,nk = 1, 2, \ldots, n, answer the following question. Suppose that only the first kk monsters, with numbers 1,2,…,k1, 2, \ldots, k, are present in the game. What is the smallest number of times you need to cast spells of type 1 to kill all kk monsters?

这是该问题的困难版本。在本版本中,你需要对怪物数组的每个前缀求解答案。

在一款电脑游戏中,你正在与 nn 个怪物战斗。第 ii 个怪物有 aia_i 点生命值,所有 aia_i 均为整数。只要一个怪物的生命值至少为 11,它就仍处于存活状态。

你可以施放两种类型的法术:

  1. 对任意一个你选择的存活怪物造成 11 点伤害;
  2. 对所有存活怪物各造成 11 点伤害。若此次施法导致至少一个怪物死亡(即其生命值恰好变为 00),则立即重复该法术(并持续重复,直到某次施法不再导致任何怪物死亡为止)。

对一个怪物造成 11 点伤害会使其生命值减少 11。

类型 1 的法术可以施放任意多次,而类型 2 的法术在整个游戏中最多只能施放一次。

对每个 k=1,2,…,nk = 1, 2, \ldots, n,回答以下问题:假设游戏中仅有编号为 1,2,…,k1, 2, \ldots, k 的前 kk 个怪物存在,那么要消灭全部 kk 个怪物,你最少需要施放多少次类型 1 的法术?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

Each test case consists of two lines. The first line contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of monsters.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n) — monsters' health points.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例由两行组成。第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)——怪物的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n)——怪物的生命值。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print nn integers. The kk-th of these integers must be equal to the smallest number of times you need to cast spells of type 1 to kill all kk monsters, if only monsters with numbers 1,2,…,k1, 2, \ldots, k are present in the game.

对于每个测试用例,输出 nn 个整数。其中第 kk 个整数必须等于:当游戏中仅存在编号为 1,2,…,k1, 2, \ldots, k 的怪物时,杀死全部 kk 只怪物所需施放类型 1 法术的最少次数。

输入输出样例

  • 输入#1

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

    输出#1

    2 1 0
    3 2 4 4 4 4

说明/提示

In the first test case, for k=nk = n, the initial health points of the monsters are [3,1,2][3, 1, 2]. It is enough to cast a spell of type 2:

  • Monsters' health points change to [2,0,1][2, 0, 1]. Since monster number 22 dies, the spell is repeated.
  • Monsters' health points change to [1,0,0][1, 0, 0]. Since monster number 33 dies, the spell is repeated.
  • Monsters' health points change to [0,0,0][0, 0, 0]. Since monster number 11 dies, the spell is repeated.
  • Monsters' health points change to [0,0,0][0, 0, 0].

Since it is possible to use no spells of type 1 at all, the answer is 00.

In the second test case, for k=nk = n, the initial health points of the monsters are [4,1,5,4,1,1][4, 1, 5, 4, 1, 1]. Here is one of the optimal action sequences:

  • Using a spell of type 1, deal 11 damage to monster number 11. Monsters' health points change to [3,1,5,4,1,1][3, 1, 5, 4, 1, 1].
  • Using a spell of type 1, deal 11 damage to monster number 44. Monsters' health points change to [3,1,5,3,1,1][3, 1, 5, 3, 1, 1].
  • Using a spell of type 1, deal 11 damage to monster number 44 again. Monsters' health points change to [3,1,5,2,1,1][3, 1, 5, 2, 1, 1].
  • Use a spell of type 2:
    • Monsters' health points change to [2,0,4,1,0,0][2, 0, 4, 1, 0, 0]. Since monsters number 22, 55, and 66 die, the spell is repeated.
    • Monsters' health points change to [1,0,3,0,0,0][1, 0, 3, 0, 0, 0]. Since monster number 44 dies, the spell is repeated.
    • Monsters' health points change to [0,0,2,0,0,0][0, 0, 2, 0, 0, 0]. Since monster number 11 dies, the spell is repeated.
    • Monsters' health points change to [0,0,1,0,0,0][0, 0, 1, 0, 0, 0].
  • Using a spell of type 1, deal 11 damage to monster number 33. Monsters' health points change to [0,0,0,0,0,0][0, 0, 0, 0, 0, 0].

Spells of type 1 are cast 44 times in total. It can be shown that this is the smallest possible number.

在第一个测试用例中,当 k=nk = n 时,怪物的初始生命值为 [3,1,2][3, 1, 2]。仅需施放一次类型 2 的法术即可:

  • 怪物的生命值变为 [2,0,1][2, 0, 1]。由于编号为 22 的怪物死亡,该法术被重复施放。
  • 怪物的生命值变为 [1,0,0][1, 0, 0]。由于编号为 33 的怪物死亡,该法术被重复施放。
  • 怪物的生命值变为 [0,0,0][0, 0, 0]。由于编号为 11 的怪物死亡,该法术被重复施放。
  • 怪物的生命值变为 [0,0,0][0, 0, 0]。

由于完全可以不使用任何类型 1 的法术,因此答案为 00。

在第二个测试用例中,当 k=nk = n 时,怪物的初始生命值为 [4,1,5,4,1,1][4, 1, 5, 4, 1, 1]。以下是一种最优操作序列:

  • 施放一次类型 1 的法术,对编号为 11 的怪物造成 11 点伤害。怪物的生命值变为 [3,1,5,4,1,1][3, 1, 5, 4, 1, 1]。
  • 施放一次类型 1 的法术,对编号为 44 的怪物造成 11 点伤害。怪物的生命值变为 [3,1,5,3,1,1][3, 1, 5, 3, 1, 1]。
  • 再次施放一次类型 1 的法术,对编号为 44 的怪物造成 11 点伤害。怪物的生命值变为 [3,1,5,2,1,1][3, 1, 5, 2, 1, 1]。
  • 施放一次类型 2 的法术:
    • 怪物的生命值变为 [2,0,4,1,0,0][2, 0, 4, 1, 0, 0]。由于编号为 22、55 和 66 的怪物死亡,该法术被重复施放。
    • 怪物的生命值变为 [1,0,3,0,0,0][1, 0, 3, 0, 0, 0]。由于编号为 44 的怪物死亡,该法术被重复施放。
    • 怪物的生命值变为 [0,0,2,0,0,0][0, 0, 2, 0, 0, 0]。由于编号为 11 的怪物死亡,该法术被重复施放。
    • 怪物的生命值变为 [0,0,1,0,0,0][0, 0, 1, 0, 0, 0]。
  • 施放一次类型 1 的法术,对编号为 33 的怪物造成 11 点伤害。怪物的生命值变为 [0,0,0,0,0,0][0, 0, 0, 0, 0, 0]。

类型 1 的法术总共被施放了 44 次。可以证明这是可能的最小次数。

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

首页