Codeforces Round 960 (Div. 2)
D
Grid Puzzle
Codeforces
1800
原题
← CE1 →
翻译中…

You are given an array $a$ of size $n$.

翻译中…

There is an $n \times n$ grid. In the $i$-th row, the first $a_i$ cells are black and the other cells are white. In other words, note $(i,j)$ as the cell in the $i$-th row and $j$-th column, cells $(i,1), (i,2), \ldots, (i,a_i)$ are black, and cells $(i,a_i+1), \ldots, (i,n)$ are white.

翻译中…

You can do the following operations any number of times in any order:

翻译中…
  • Dye a $2 \times 2$ subgrid white;
  • Dye a whole row white. Note you can not dye a whole column white.
翻译中…

Find the minimum number of operations to dye all cells white.

翻译中…

Input

翻译中…

The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.

翻译中…

For each test case:

翻译中…
  • The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the size of the array $a$.
  • The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq n$).
翻译中…

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

翻译中…

Output

翻译中…

For each test case, output a single integer — the minimum number of operations to dye all cells white.

翻译中…

Note

翻译中…

In the first test case, you don't need to do any operation.

翻译中…

In the second test case, you can do:

翻译中…
  • Dye $(1,1), (1,2), (2,1)$, and $(2,2)$ white;
  • Dye $(2,3), (2,4), (3,3)$, and $(3,4)$ white;
  • Dye $(3,1), (3,2), (4,1)$, and $(4,2)$ white.
翻译中…

It can be proven $3$ is the minimum number of operations.

翻译中…

In the third test case, you can do:

翻译中…
  • Dye the first row white;
  • Dye $(2,1), (2,2), (3,1)$, and $(3,2)$ white.
翻译中…

It can be proven $2$ is the minimum number of operations.

翻译中…

样例

Input #1
10
1
0
4
2 4 4 2
4
3 2 1 0
3
0 3 0
3
0 1 3
3
3 1 0
4
3 1 0 3
4
0 2 2 2
6
1 3 4 2 0 4
8
2 2 5 2 3 4 2 4
Output #1
0
3
2
1
2
2
3
2
4
6
InputCopy OutputCopy
题目信息
题号
1990D
时间限制
2 s
内存限制
256 MB
标签
位运算
暴力
动态规划
贪心
模拟

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