CF1746B.Rebellion

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have an array aa of size nn consisting only of zeroes and ones. You can do the following operation:

  • choose two indices 1≤i,j≤n1 \le i , j \le n, i≠ji \ne j,
  • add aia_{i} to aja_{j},
  • remove aia_{i} from aa.

Note that elements of aa can become bigger than 11 after performing some operations. Also note that nn becomes 11 less after the operation.

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

你有一个长度为 nn 的数组 aa,其中仅包含 00 和 11。你可以执行以下操作:

  • 选择两个下标 1≤i,j≤n1 \le i , j \le n,且 i≠ji \ne j,
  • 将 aia_{i} 加到 aja_{j} 上,
  • 从 aa 中删除 aia_{i}。

注意:执行若干次操作后,aa 中的元素可能大于 11。此外,每次操作后 nn 减少 11。

要使 aa 变为非递减(即每个元素都不小于前一个元素),所需的最少操作次数是多少?

输入格式

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

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

Next line contains nn integers a1,a2,…ana_{1}, a_{2}, \ldots a_{n} (aia_i is 00 or 11), elements of array aa.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),表示数组 aa 的大小。

下一行包含 nn 个整数 a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n}(每个 aia_i 为 00 或 11),即数组 aa 的元素。

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

输出格式

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

对于每个测试用例,输出一个整数,表示使 aa 非递减所需的最少操作次数。

输入输出样例

  • 输入#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

    输出#1

    0
    1
    1
    3

说明/提示

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

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

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

在第一个测试用例中,aa 已经是非递减的,因此无需执行任何操作,答案为 00。

在第二个测试用例中,你可以对 i=1i = 1 和 j=5j = 5 执行一次操作,使得 aa 变为 [0,0,1,2][0, 0, 1, 2],此时它成为非递减序列。

在第三个测试用例中,你可以对 i=2i = 2 和 j=1j = 1 执行一次操作,使得 aa 变为 [1][1],此时它成为非递减序列。

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

首页