CF1693F.I Might Be Wrong

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a binary string SS of length nn indexed from 11 to nn . You can perform the following operation any number of times (possibly zero):

  • Choose two integers ll and rr ( 1lrn1 \le l \le r \le n ). Let cnt0cnt_0 be the number of times 0 occurs in S[lr]S[l \ldots r] and cnt1cnt_1 be the number of times 1 occurs in S[lr]S[l \ldots r] . You can pay cnt0cnt1+1|cnt_0 - cnt_1| + 1 coins and sort the S[lr]S[l \ldots r] . (by S[lr]S[l \ldots r] we mean the substring of SS starting at position ll and ending at position rr )

    For example if $S = $ 11001, we can perform the operation on S[24]S[2 \ldots 4] , paying 21+1=2|2 - 1| + 1 = 2 coins, and obtain $S = $ 10011 as a new string.

Find the minimum total number of coins required to sort SS in increasing order.

输入格式

The first line contains a single integer tt ( 1t10001 \le t \le 1000 ) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn ( 1n21051 \le n \le 2 \cdot 10^5 ) — the size of SS .

The second line of each test case contains a binary string SS of nn characters S1S2SnS_1S_2 \ldots S_n . ( $S_i = $ 0 or $S_i = $ 1 for each 1in1 \le i \le n )

It is guaranteed that the sum of nn over all test cases doesn't exceed 21052 \cdot 10^5 .

输出格式

For each test case, output the minimum total number of coins required to sort SS 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, SS is already sorted.

In the second test case, it's enough to apply the operation with l=1,r=2l = 1, r = 2 .

In the third test case, it's enough to apply the operation with l=1,r=2l = 1, r = 2 .

首页