CF1693F.I Might Be Wrong
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a binary string S of length n indexed from 1 to n . You can perform the following operation any number of times (possibly zero):
-
Choose two integers l and r ( 1≤l≤r≤n ). Let cnt0 be the number of times 0 occurs in S[l…r] and cnt1 be the number of times 1 occurs in S[l…r] . You can pay ∣cnt0−cnt1∣+1 coins and sort the S[l…r] . (by S[l…r] we mean the substring of S starting at position l and ending at position r )
For example if $S = $ 11001, we can perform the operation on S[2…4] , paying ∣2−1∣+1=2 coins, and obtain $S = $ 10011 as a new string.
Find the minimum total number of coins required to sort S in increasing order.
输入格式
The first line contains a single integer t ( 1≤t≤1000 ) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤2⋅105 ) — the size of S .
The second line of each test case contains a binary string S of n characters S1S2…Sn . ( $S_i = $ 0 or $S_i = $ 1 for each 1≤i≤n )
It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅105 .
输出格式
For each test case, output the minimum total number of coins required to sort S in increasing order.
输入输出样例
输入#1
7 1 1 2 10 3 101 4 1000 5 11010 6 110000 20 01000010001010011000
输出#1
0 1 1 3 2 2 5
说明/提示
In the first test case, S is already sorted.
In the second test case, it's enough to apply the operation with l=1,r=2 .
In the third test case, it's enough to apply the operation with l=1,r=2 .