CF2266C.AND, OR, Sort!
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a binary string∗ s of length n.
You may perform the following operation any number of times (possibly zero):
- choose an integer i (1≤i≤n), and replace si with either the bitwise AND or the bitwise OR of s1,s2,…,si.
Note that the bitwise AND or bitwise OR of a single element is equal to the element itself.
Your goal is to make s sorted in non-decreasing order†.
Find the minimum number of operations required to sort s in non-decreasing order.
∗A binary string only contains characters 0 and 1.
†If s is in non-decreasing order, then s1≤s2≤…≤sn.
给你一个长度为 n 的二进制字符串∗ s。
你可以执行以下操作任意次(可以为零次):
注意:单个元素的按位与或按位或结果即为该元素本身。
你的目标是使 s 按非递减顺序† 排列。
求使 s 变为非递减顺序所需的最少操作次数。
∗二进制字符串仅包含字符 0 和 1。
†若 s 是非递减顺序的,则满足 s1≤s2≤…≤sn。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the length of the binary string s.
The second line of each test case contains the binary string s of length n. Each character of s is either 0 or 1.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)—— 二进制字符串 s 的长度。
每个测试用例的第二行包含长度为 n 的二进制字符串 s,其中 s 的每个字符均为 0 或 1。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, print a single integer — the minimum number of operations required to sort s in non-decreasing order.
对于每个测试用例,输出一个整数——将 s 按非递减顺序排序所需的最少操作次数。
输入输出样例
输入#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 3 operations.
在第一个测试用例中,字符串已经排好序,因此不需要执行任何操作。
在第二个测试用例中,我们可以使用按位或(bitwise OR)操作将最后三个字符变为 1,从而在 3 次操作内得到 1111。
输入解题思路,AI测评打分。不知道怎么写?