CF1795E.Explosions?

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are playing yet another game where you kill monsters using magic spells. There are nn cells in the row, numbered from 11 to nn. Initially, the ii-th cell contains the ii-th monster with hih_i health.

You have a basic spell that costs 11 MP and deals 11 damage to the monster you choose. You can cast it any number of times. Also, you have a special scroll with "Explosion" spell you can use only once. You want to finish killing monsters with explosion, that's why you, firstly, cast the basic spell several times (possibly, zero), and then after that, you cast one "Explosion".

How does "Explosion" spell work? Firstly, you choose the power of the spell: if you pour xx MP into it, "Explosion" will deal xx damage. Secondly, you choose some monster ii, which will be targeted by the spell. That's what happens next:

  • if its current health hi>xh_i \gt x, then he stays alive with health decreased by xx;
  • if hi≤xh_i \le x, the ii-th monster dies with an explosion that deals hi−1h_i - 1 damage to monsters in the neighboring cells i−1i - 1 and i+1i + 1, if these cells exist and monsters inside are still alive;
  • if the damage dealt by the explosion is enough to kill the monster i−1i - 1 (or i+1i + 1), i. e. the current hi−1≤hi−1h_{i - 1} \le h_i - 1 (or hi+1≤hi−1h_{i + 1} \le h_i - 1), then that monster also dies creating a secondary explosion of power hi−1−1h_{i-1} - 1 (or hi+1−1h_{i+1} - 1) that may deals damage to their neighbors, and so on, until the explosions end.

Your goal is to kill all the remaining monsters with those "chaining" explosions, that's why you need a basic spell to decrease hih_i of some monsters or even kill them beforehand (monsters die when their current health hih_i becomes less or equal to zero). Note that monsters don't move between cells, so, for example, monsters ii and i+2i + 2 will never become neighbors.

What is the minimum total MP you need to kill all monsters in the way you want? The total MP is counted as the sum of the number of basic spells you cast and the power xx of explosion scroll you've chosen.

你正在玩另一款使用魔法消灭怪物的游戏。一行中有 nn 个格子,编号从 11 到 nn。初始时,第 ii 个格子中包含第 ii 只怪物,其生命值为 hih_i。

你拥有一种基础法术:消耗 11 点法力值(MP),对任意一只选定的怪物造成 11 点伤害。该法术可施放任意次数(包括零次)。此外,你还拥有一张特殊的卷轴,上面记载着“爆炸”法术,你仅能使用一次。你希望最终通过“爆炸”法术消灭所有怪物,因此你的策略是:先施放若干次(可能为零次)基础法术,然后施放一次“爆炸”法术。

“爆炸”法术如何生效?首先,你需要决定该法术的威力:若向其中注入 xx 点 MP,则“爆炸”将造成 xx 点伤害;其次,你需要选择一只目标怪物 ii。接下来发生如下事件:

  • 若该怪物当前生命值 hi>xh_i > x,则它存活,生命值减少 xx;
  • 若 hi≤xh_i \le x,则第 ii 只怪物死亡,并引发一次爆炸,对相邻格子(即 i−1i-1 和 i+1i+1)中的怪物各造成 hi−1h_i - 1 点伤害(前提是这些格子存在,且其中的怪物仍存活);
  • 若爆炸所造成的伤害足以杀死怪物 i−1i-1(或 i+1i+1),即其当前生命值满足 hi−1≤hi−1h_{i-1} \le h_i - 1(或 hi+1≤hi−1h_{i+1} \le h_i - 1),则该怪物也死亡,并引发一次次级爆炸,其威力为 hi−1−1h_{i-1} - 1(或 hi+1−1h_{i+1} - 1),进而可能对其邻居造成伤害;如此连锁进行,直至所有爆炸终止。

你的目标是:仅依靠这些“连锁爆炸”消灭所有剩余怪物。因此,你需要预先使用基础法术降低某些怪物的生命值(甚至提前将其击杀——当怪物当前生命值 hi≤0h_i \le 0 时即视为死亡)。注意,怪物不会在格子间移动,例如,怪物 ii 与 i+2i+2 永远不会成为邻居。

请问:按上述方式消灭所有怪物所需的最小总法力值是多少?总法力值定义为:你施放的基础法术次数 + 你为“爆炸”卷轴选定的威力 xx。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains the single integer nn (1≤n≤3⋅1051 \le n \le 3 \cdot 10^5) — the number of cells in the row, i. e. the number of monsters.

The second line of each test case contains nn integers h1,h2,…,hnh_1, h_2, \dots, h_n (1≤hi≤1061 \le h_i \le 10^6) — the initial health of the monsters.

It's guaranteed that the sum of nn over all test cases doesn't exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤3⋅1051 \le n \le 3 \cdot 10^5)—— 行中单元格的数量,即怪物的数量。

每个测试用例的第二行包含 nn 个整数 h1,h2,…,hnh_1, h_2, \dots, h_n(1≤hi≤1061 \le h_i \le 10^6)—— 怪物的初始生命值。

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

输出格式

For each test case, print one integer — the minimum total MP you need to kill all monsters by finishing them with explosion.

对于每个测试用例,输出一个整数——即通过爆炸终结所有怪物所需的最小总魔法值(MP)。

输入输出样例

  • 输入#1

    5
    3
    1 1 1
    4
    4 1 2 1
    4
    5 10 15 10
    1
    42
    9
    1 2 3 2 2 2 3 2 1

    输出#1

    3
    6
    15
    42
    12

说明/提示

In the first test case, you can, for example, use basic spell on monsters 11 and 22 (once per monster) to kill them. After that, you cast "Explosion" of power x=1x = 1 on monster 33 to kill it. The total MP you need is 2+1=32 + 1 = 3.

In the second test case, it's optimal to cast basic spell 44 times onto monster 11 to kill it. After that, you can cast "Explosion" of power x=2x = 2 onto monster 33. It dies, creating an explosion of power 11 that kills monsters 22 and 44. The total MP you need is 4+2=64 + 2 = 6.

In the third test case, you cast "Explosion" of power 1515 onto monster 33. Explosion of the 33-rd monster (of power 1414) kills monsters 22 and 44. Secondary explosion of monster 22 (of power 99) kills monster 11.

在第一个测试用例中,例如,你可以对怪物 11 和 22 各使用一次基础法术来击杀它们。之后,你对怪物 33 施放一次威力为 x=1x = 1 的“爆炸”法术来击杀它。总共需要的法力值(MP)为 2+1=32 + 1 = 3。

在第二个测试用例中,最优策略是向怪物 11 施放 44 次基础法术来击杀它。之后,你向怪物 33 施放一次威力为 x=2x = 2 的“爆炸”法术。该怪物死亡并引发一次威力为 11 的爆炸,从而击杀怪物 22 和 44。总共需要的法力值(MP)为 4+2=64 + 2 = 6。

在第三个测试用例中,你向怪物 33 施放一次威力为 1515 的“爆炸”法术。怪物 33 死亡并引发一次威力为 1414 的爆炸,从而击杀怪物 22 和 44;随后怪物 22 死亡并引发一次威力为 99 的次级爆炸,从而击杀怪物 11。

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

首页