CF1706C.Qpwoeirut And The City

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Qpwoeirut has taken up architecture and ambitiously decided to remodel his city.

Qpwoeirut's city can be described as a row of nn buildings, the ii-th (1≤i≤n1 \le i \le n) of which is hih_i floors high. You can assume that the height of every floor in this problem is equal. Therefore, building ii is taller than the building jj if and only if the number of floors hih_i in building ii is larger than the number of floors hjh_j in building jj.

Building ii is cool if it is taller than both building i−1i-1 and building i+1i+1 (and both of them exist). Note that neither the 11-st nor the nn-th building can be cool.

To remodel the city, Qpwoeirut needs to maximize the number of cool buildings. To do this, Qpwoeirut can build additional floors on top of any of the buildings to make them taller. Note that he cannot remove already existing floors.

Since building new floors is expensive, Qpwoeirut wants to minimize the number of floors he builds. Find the minimum number of floors Qpwoeirut needs to build in order to maximize the number of cool buildings.

Qpwoeirut 开始学习建筑学,并雄心勃勃地决定改造他的城市。

Qpwoeirut 的城市可以被描述为一排 nn 座建筑,其中第 ii 座(1≤i≤n1 \le i \le n)建筑高 hih_i 层。你可以假设本题中每层楼的高度均相等。因此,当且仅当第 ii 座建筑的楼层数 hih_i 大于第 jj 座建筑的楼层数 hjh_j 时,第 ii 座建筑才比第 jj 座建筑更高。

若第 ii 座建筑比其左右相邻的两座建筑(即第 i−1i-1 座和第 i+1i+1 座建筑)都更高(且这两座相邻建筑均存在),则称第 ii 座建筑是“酷”的。注意:第 11 座和第 nn 座建筑不可能是“酷”的。

为了改造城市,Qpwoeirut 需要使“酷”建筑的数量最大化。为此,他可以在任意建筑顶部增建若干楼层,使其变高(但不能拆除已有的楼层)。

由于增建楼层成本高昂,Qpwoeirut 希望最小化所增建的楼层数。请找出在使“酷”建筑数量最大化的前提下,Qpwoeirut 所需增建的最少楼层数。

输入格式

The first line contains a single 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 (3≤n≤1053 \le n \le 10^5) — the number of buildings in Qpwoeirut's city.

The second line of each test case contains nn integers h1,h2,…,hnh_1, h_2, \ldots, h_n (1≤hi≤1091 \le h_i \le 10^9) — the number of floors in each of the buildings of the city.

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(3≤n≤1053 \le n \le 10^5)—— Qpwoeirut 所在城市中建筑物的数量。

每个测试用例的第二行包含 nn 个整数 h1,h2,…,hnh_1, h_2, \ldots, h_n(1≤hi≤1091 \le h_i \le 10^9)—— 城市中每栋建筑物的楼层数。

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

输出格式

For each test case, print a single integer — the minimum number of additional floors Qpwoeirut needs to build in order to maximize the number of cool buildings.

对于每个测试用例,输出一个整数——Qpwoeirut 为使“酷建筑”的数量最大化而需要额外建造的最少楼层数。

输入输出样例

  • 输入#1

    6
    3
    2 1 2
    5
    1 2 1 4 3
    6
    3 1 4 5 5 2
    8
    4 2 1 3 5 3 6 1
    6
    1 10 1 1 10 1
    8
    1 10 11 1 10 11 10 1

    输出#1

    2
    0
    3
    3
    0
    4

说明/提示

In the first test case, it is optimal for Qpwoeirut to make the second building cool by building 22 additional floors on top of it, making it taller than both of its adjacent buildings. The final heights of buildings will be [2,3‾,2][2, \underline{3}, 2].

In the second test case, the number of cool buildings is already maximized, so Qpwoeirut does not need to do anything.

In the third test case, it is optimal for Qpwoeirut to make the third and fifth buildings cool by building 22 additional floors onto the third building and 11 additional floor onto the fifth building. The final heights of buildings will be [3,1,6‾,5,6‾,2][3, 1, \underline{6}, 5, \underline{6}, 2].

It can be shown that it is impossible to make more than 22 of the buildings cool, or to make 22 buildings cool using fewer than 33 additional floors.

In the fourth test case, Qpwoeirut can either make the second building cool, or he can make the third building cool. Either way, he will be building 33 additional floors and maximizing the number of cool buildings. The final heights of buildings will be [4,2,4‾,3,5,3,6,1][4, 2, \underline{4}, 3, 5, 3, 6, 1] or [4,5‾,1,3,5,3,6,1][4, \underline{5}, 1, 3, 5, 3, 6, 1].

在第一个测试用例中,Qpwoeirut 的最优策略是在第二栋建筑上额外建造 22 层,使其高于其两侧相邻的建筑,从而使其变得“酷”。最终各建筑的高度为 [2,3‾,2][2, \underline{3}, 2]。

在第二个测试用例中,“酷”建筑的数量已达到最大值,因此 Qpwoeirut 无需进行任何操作。

在第三个测试用例中,Qpwoeirut 的最优策略是让第三栋和第五栋建筑变得“酷”:在第三栋建筑上额外建造 22 层,在第五栋建筑上额外建造 11 层。最终各建筑的高度为 [3,1,6‾,5,6‾,2][3, 1, \underline{6}, 5, \underline{6}, 2]。

可以证明:无法使超过 22 栋建筑变得“酷”,也无法仅用少于 33 层的额外楼层就使 22 栋建筑变得“酷”。

在第四个测试用例中,Qpwoeirut 可选择让第二栋建筑变得“酷”,也可选择让第三栋建筑变得“酷”。无论哪种方式,他都需要额外建造 33 层,并且都能使“酷”建筑的数量达到最大值。最终各建筑的高度为 [4,2,4‾,3,5,3,6,1][4, 2, \underline{4}, 3, 5, 3, 6, 1] 或 [4,5‾,1,3,5,3,6,1][4, \underline{5}, 1, 3, 5, 3, 6, 1]。

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

首页