Codeforces 第 1129 轮(Div. 2)
B
序列递减
Sequence Decreasing
Codeforces
未评级
原题
← AC →

序列递减AI (CI翻译)

An array $b$ of non-negative integers is called good if it can be turned into a non-empty array consisting only of zeros by performing the following operation any number of times (possibly zero):

如果一个由非负整数组成的数组 $b$ 可以通过执行任意次(可以为零次)以下操作,变成一个非空且所有元素均为零的数组,则称 $b$ 为好数组:AI (CI翻译)

  1. choose a non-empty subarray$^{\text{∗}}$ of $b$ in which all elements are positive;
  2. decrease each element of this subarray by $1$;
  3. choose one element of this subarray and remove it from $b$, without changing the order of the remaining elements.
  1. 选择 $b$ 的一个非空子数组$^{\text{∗}}$,其中所有元素均为正数;
  2. 将该子数组中的每个元素减去 $1$;
  3. 选择该子数组中的一个元素,将其从 $b$ 中删除,且不改变其余元素的顺序。
AI (CI翻译)

You are given an array $a$ of $n$ non-negative integers. Find the maximum length of a good subsequence$^{\text{†}}$ of $a$, or report that $a$ has no good subsequence.

给定一个由 $n$ 个非负整数组成的数组 $a$。请找出 $a$ 的所有好子序列$^{\text{†}}$中的最大长度,或报告 $a$ 不存在好子序列。AI (CI翻译)

$^{\text{∗}}$An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.$^{\text{†}}$A sequence $c$ is a subsequence of a sequence $a$ if $c$ can be obtained from $a$ by the deletion of several (possibly, zero or all) elements from arbitrary positions.

$^{\text{∗}}$如果数组 $c$ 可以通过从数组 $b$ 的开头删除若干个(可以为零个或全部)元素,再从末尾删除若干个(可以为零个或全部)元素得到,则称 $c$ 是 $b$ 的一个子数组。$^{\text{†}}$如果序列 $c$ 可以通过从序列 $a$ 的任意位置删除若干个(可以为零个或全部)元素得到,则称 $c$ 是 $a$ 的一个子序列。AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

每组输入包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是各个测试用例的描述。AI (CI翻译)

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.

每个测试用例的第一行包含一个整数 $n$($1 \le n \le 2 \cdot 10^5$)——数组 $a$ 的长度。AI (CI翻译)

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the array $a$.

每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($0 \le a_i \le 10^9$)——数组 $a$ 的元素。AI (CI翻译)

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

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a single integer — the maximum length of a good subsequence of $a$, or $0$ if no such subsequence exists.

对于每个测试用例,输出一个整数——$a$ 的好子序列的最大长度;如果不存在这样的子序列,则输出 $0$。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the array $a$ itself is good, since it is non-empty and consists only of zeros. Thus, the answer is $1$.

在第一个测试用例中,数组 $a$ 本身就是好数组,因为它非空且所有元素均为零。因此,答案为 $1$。AI (CI翻译)

In the second test case, it can be shown that $a$ has no good subsequence. Thus, the answer is $0$.

在第二个测试用例中,可以证明 $a$ 不存在好子序列。因此,答案为 $0$。AI (CI翻译)

样例

Input #1
4
1
0
1
1
3
5 3 4
5
2 1 1 0 3
Output #1
1
0
0
5
InputCopy OutputCopy
题目信息
题号
2277B
时间限制
1 s
内存限制
256 MB
标签
构造
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。