CF2266C.AND, OR, Sort!

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a binary string∗^{\text{∗}} ss of length nn.

You may perform the following operation any number of times (possibly zero):

  • choose an integer ii (1≤i≤n1 \le i \le n), and replace sis_i with either the bitwise AND or the bitwise OR of s1,s2,…,sis_1, s_2, \ldots, s_i.

Note that the bitwise AND or bitwise OR of a single element is equal to the element itself.

Your goal is to make ss sorted in non-decreasing order†^{\text{†}}.

Find the minimum number of operations required to sort ss in non-decreasing order.

∗^{\text{∗}}A binary string only contains characters 0\texttt{0} and 1\texttt{1}.

†^{\text{†}}If ss is in non-decreasing order, then s1≤s2≤…≤sns_1 \leq s_2 \leq \ldots \leq s_n.

给你一个长度为 nn 的二进制字符串∗^{\text{∗}} ss。

你可以执行以下操作任意次(可以为零次):

  • 选择一个整数 ii(1≤i≤n1 \le i \le n),并将 sis_i 替换为 s1,s2,…,sis_1, s_2, \ldots, s_i 的按位与或按位或结果。

注意:单个元素的按位与或按位或结果即为该元素本身。

你的目标是使 ss 按非递减顺序†^{\text{†}} 排列。

求使 ss 变为非递减顺序所需的最少操作次数。

∗^{\text{∗}}二进制字符串仅包含字符 0\texttt{0} 和 1\texttt{1}。

†^{\text{†}}若 ss 是非递减顺序的,则满足 s1≤s2≤…≤sns_1 \leq s_2 \leq \ldots \leq s_n。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the length of the binary string ss.

The second line of each test case contains the binary string ss of length nn. Each character of ss is either 0 or 1.

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

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 二进制字符串 ss 的长度。

每个测试用例的第二行包含长度为 nn 的二进制字符串 ss,其中 ss 的每个字符均为 0 或 1。

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

输出格式

For each test case, print a single integer — the minimum number of operations required to sort ss in non-decreasing order.

对于每个测试用例,输出一个整数——将 ss 按非递减顺序排序所需的最少操作次数。

输入输出样例

  • 输入#1

    6
    4
    0011
    4
    1000
    5
    01000
    8
    01001101
    7
    0101010
    7
    0111101

    输出#1

    0
    3
    1
    2
    3
    1

说明/提示

In the first test case, the string is already sorted, so no operations are required.

In the second test case, we can use bitwise OR to change the last three characters to 1, obtaining 1111 in 33 operations.

在第一个测试用例中,字符串已经排好序,因此不需要执行任何操作。

在第二个测试用例中,我们可以使用按位或(bitwise OR)操作将最后三个字符变为 1,从而在 33 次操作内得到 1111。

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

首页