310488: CF1841E. Fill the Matrix

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Fill the Matrixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

There is a square matrix, consisting of $n$ rows and $n$ columns of cells, both numbered from $1$ to $n$. The cells are colored white or black. Cells from $1$ to $a_i$ are black, and cells from $a_i+1$ to $n$ are white, in the $i$-th column.

You want to place $m$ integers in the matrix, from $1$ to $m$. There are two rules:

  • each cell should contain at most one integer;
  • black cells should not contain integers.

The beauty of the matrix is the number of such $j$ that $j+1$ is written in the same row, in the next column as $j$ (in the neighbouring cell to the right).

What's the maximum possible beauty of the matrix?

Input

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.

The first line of each testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the size of the matrix.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — the number of black cells in each column.

The third line contains a single integer $m$ ($0 \le m \le \sum \limits_{i=1}^n n - a_i$) — the number of integers you have to write in the matrix. Note that this number might not fit into a 32-bit integer data type.

The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$.

Output

For each testcase, print a single integer — the maximum beauty of the matrix after you write all $m$ integers in it. Note that there are no more integers than the white cells, so the answer always exists.

ExampleInput
6
3
0 0 0
9
4
2 0 3 1
5
4
2 0 3 1
6
4
2 0 3 1
10
10
0 2 2 1 5 10 3 4 1 1
20
1
1
0
Output
6
3
4
4
16
0

Input

题意翻译

有一个 $n$ 行 $n$ 列的矩阵,行和列都从 $1$ 到 $n$ 编号。对于第 $i$ **列**,第 $1 \sim a_i$ 个格子是黑色的,其余格子是白色的。 你可以把 $1 \sim m$ 共 $m$ 个整数放到矩阵里,满足两条规则: 1. 每个格子包含至多一个整数。 2. 黑色格子不能包含整数。 一个矩阵的美丽程度是满足这样的格子个数:该格子自身包含一个数 $j$,且它右边与它相邻的格子包含的数为 $j + 1$。 请求出矩阵的最大美丽值。 多组数据,$n$ 之和不超过 $2 \times 10^5$,请注意 $m$ 的大小可能超过 32 位整数所存储的范围。 Translated by 一扶苏一

Output

题目大意:
有一个由n行n列组成的方形矩阵,每个格子要么是黑色要么是白色。第i列中,从1到a_i的格子是黑色的,从a_i+1到n的格子是白色的。要在矩阵中放入m个整数(从1到m),遵循两个规则:每个格子最多放一个整数;黑色格子不能放整数。矩阵的“美感”是指满足j+1这个整数出现在j的右邻格中的j的数量。问矩阵的最大可能“美感”是多少?

输入输出数据格式:
输入:
- 第一行是一个整数t(1≤t≤10^4),表示测试用例的数量。
- 每个测试用例的第一行是一个整数n(1≤n≤2×10^5),表示矩阵的大小。
- 每个测试用例的第二行包含n个整数a_1, a_2, ..., a_n(0≤a_i≤n),表示每列中黑色格子的数量。
- 每个测试用例的第三行是一个整数m(0≤m≤Σ_{i=1}^n (n - a_i)),表示要放入矩阵中的整数的数量。注意这个数可能不适合32位整数数据类型。
- 所有测试用例的n之和不超过2×10^5。

输出:
- 对于每个测试用例,输出一个整数,表示放入所有m个整数后矩阵的最大“美感”。

示例输入输出:
输入:
```
6
3
0 0 0
9
4
2 0 3 1
5
4
2 0 3 1
6
4
2 0 3 1
10
0 2 2 1 5 10 3 4 1 1
20
1
1
0
```
输出:
```
6
3
4
4
16
0
```题目大意: 有一个由n行n列组成的方形矩阵,每个格子要么是黑色要么是白色。第i列中,从1到a_i的格子是黑色的,从a_i+1到n的格子是白色的。要在矩阵中放入m个整数(从1到m),遵循两个规则:每个格子最多放一个整数;黑色格子不能放整数。矩阵的“美感”是指满足j+1这个整数出现在j的右邻格中的j的数量。问矩阵的最大可能“美感”是多少? 输入输出数据格式: 输入: - 第一行是一个整数t(1≤t≤10^4),表示测试用例的数量。 - 每个测试用例的第一行是一个整数n(1≤n≤2×10^5),表示矩阵的大小。 - 每个测试用例的第二行包含n个整数a_1, a_2, ..., a_n(0≤a_i≤n),表示每列中黑色格子的数量。 - 每个测试用例的第三行是一个整数m(0≤m≤Σ_{i=1}^n (n - a_i)),表示要放入矩阵中的整数的数量。注意这个数可能不适合32位整数数据类型。 - 所有测试用例的n之和不超过2×10^5。 输出: - 对于每个测试用例,输出一个整数,表示放入所有m个整数后矩阵的最大“美感”。 示例输入输出: 输入: ``` 6 3 0 0 0 9 4 2 0 3 1 5 4 2 0 3 1 6 4 2 0 3 1 10 0 2 2 1 5 10 3 4 1 1 20 1 1 0 ``` 输出: ``` 6 3 4 4 16 0 ```

加入题单

上一题 下一题 算法标签: