Codeforces Global Round 23
B
Rebellion
Codeforces
800
原题
← AC →
翻译中…

You have an array $a$ of size $n$ consisting only of zeroes and ones. You can do the following operation:

翻译中…
  • choose two indices $1 \le i , j \le n$, $i \ne j$,
  • add $a_{i}$ to $a_{j}$,
  • remove $a_{i}$ from $a$.
翻译中…

Note that elements of $a$ can become bigger than $1$ after performing some operations. Also note that $n$ becomes $1$ less after the operation.

翻译中…

What is the minimum number of operations needed to make $a$ non-decreasing, i. e. that each element is not less than the previous element?

翻译中…

Input

翻译中…

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.

翻译中…

The first line of each test case contains an integer $n$ ($1 \le n \le 10^5$), the size of array $a$.

翻译中…

Next line contains $n$ integers $a_{1}, a_{2}, \ldots a_{n}$ ($a_i$ is $0$ or $1$), elements of array $a$.

翻译中…

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

翻译中…

Output

翻译中…

For each test case print a single integer, minimum number of operations needed to make $a$ non-decreasing.

翻译中…

Note

翻译中…

In the first test case, $a$ is already non-decreasing, so you don't need to do any operations and the answer is $0$.

翻译中…

In the second test case, you can perform an operation for $i = 1$ and $j = 5$, so $a$ will be equal to $[0, 0, 1, 2]$ and it becomes non-decreasing.

翻译中…

In the third test case, you can perform an operation for $i = 2$ and $j = 1$, so $a$ will be equal to $[1]$ and it becomes non-decreasing.

翻译中…

样例

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

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