CF1392C.Omkar and Waterslide

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.

Omkar currently has nn supports arranged in a line, the ii-th of which has height aia_i. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In 11 operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add 11 to each of their heights.

Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!

An array bb is a subsegment of an array cc if bb can be obtained from cc by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.

An array b1,b2,…,bnb_1, b_2, \dots, b_n is called nondecreasing if bi≤bi+1b_i\le b_{i+1} for every ii from 11 to n−1n-1.

Omkar 正在他的水上乐园中建造一条水滑梯,他需要你的帮助,以确保以尽可能高效的方式完成建造。

Omkar 目前有 nn 根按直线排列的支撑柱,其中第 ii 根的高度为 aia_i。Omkar 希望从右向左建造他的水滑梯,因此这些支撑柱的高度必须构成一个非递减序列,才能有效支撑水滑梯。在一次操作中,Omkar 可以执行以下操作:任选一个高度非递减的连续子段,并将该子段中所有支撑柱的高度均增加 11。

请帮助 Omkar 找出他所需的最少操作次数,使得支撑柱的高度能够支撑他的水滑梯!

数组 bb 是数组 cc 的一个子段(subsegment),当且仅当 bb 可通过从 cc 的开头删除若干个(可能为零个或全部)元素、并从 cc 的末尾删除若干个(可能为零个或全部)元素而得到。

数组 b1,b2,…,bnb_1, b_2, \dots, b_n 被称为非递减的(nondecreasing),当且仅当对每个从 11 到 n−1n-1 的 ii,均有 bi≤bi+1b_i \le b_{i+1}。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001 \leq t \leq 100). Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) — the number of supports Omkar has.

The second line of each test case contains nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} (0≤ai≤109)(0 \leq a_{i} \leq 10^9) — the heights of the supports.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1001 \leq t \leq 100)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5)—— Omkar 拥有的支撑柱数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,...,ana_{1},a_{2},...,a_{n}(0≤ai≤1090 \leq a_{i} \leq 10^9)—— 各支撑柱的高度。

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

输出格式

For each test case, output a single integer — the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.

对于每个测试用例,输出一个整数——Omkar 为使其支撑结构能够支撑他的水上滑梯所需的最少操作次数。

输入输出样例

  • 输入#1

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

    输出#1

    3
    2
    0

说明/提示

The subarray with which Omkar performs the operation is bolded.

In the first test case:

  • First operation:

    [5,3,2,5]→[5,3,3,5][5, 3, \textbf{2}, 5] \to [5, 3, \textbf{3}, 5]

  • Second operation:

    [5,3,3,5]→[5,4,4,5][5, \textbf{3}, \textbf{3}, 5] \to [5, \textbf{4}, \textbf{4}, 5]

  • Third operation:

    [5,4,4,5]→[5,5,5,5][5, \textbf{4}, \textbf{4}, 5] \to [5, \textbf{5}, \textbf{5}, 5]

In the third test case, the array is already nondecreasing, so Omkar does 00 operations.

Omkar 执行操作的子数组以粗体标出。

在第一个测试用例中:

  • 第一次操作:

    [5,3,2,5]→[5,3,3,5][5, 3, \textbf{2}, 5] \to [5, 3, \textbf{3}, 5]

  • 第二次操作:

    [5,3,3,5]→[5,4,4,5][5, \textbf{3}, \textbf{3}, 5] \to [5, \textbf{4}, \textbf{4}, 5]

  • 第三次操作:

    [5,4,4,5]→[5,5,5,5][5, \textbf{4}, \textbf{4}, 5] \to [5, \textbf{5}, \textbf{5}, 5]

在第三个测试用例中,数组本身已为非递减序列,因此 Omkar 执行 00 次操作。

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

首页