CF1693F.I Might Be Wrong

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

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 (1≤l≤r≤n1 \le l \le r \le n). Let cnt0cnt_0 be the number of times 0 occurs in S[l…r]S[l \ldots r] and cnt1cnt_1 be the number of times 1 occurs in S[l…r]S[l \ldots r]. You can pay ∣cnt0−cnt1∣+1|cnt_0 - cnt_1| + 1 coins and sort the S[l…r]S[l \ldots r]. (by S[l…r]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[2…4]S[2 \ldots 4], paying ∣2−1∣+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.

给你一个长度为 nn 的二进制字符串 SS,其下标从 11 到 nn。你可以执行以下操作任意次(可以为零次):

  • 选择两个整数 ll 和 rr(满足 1≤l≤r≤n1 \le l \le r \le n)。令 cnt0cnt_0 表示子串 S[l…r]S[l \ldots r] 中字符 0 出现的次数,cnt1cnt_1 表示子串 S[l…r]S[l \ldots r] 中字符 1 出现的次数。你可以花费 ∣cnt0−cnt1∣+1|cnt_0 - cnt_1| + 1 枚硬币,将子串 S[l…r]S[l \ldots r] 升序排序。(这里 S[l…r]S[l \ldots r] 表示字符串 SS 中从位置 ll 开始、到位置 rr 结束的子串)

    例如,若 $S = $ 11001,我们可以在子串 S[2…4]S[2 \ldots 4] 上执行该操作,花费 ∣2−1∣+1=2|2 - 1| + 1 = 2 枚硬币,得到新字符串 $S = $ 10011。

求将 SS 升序排序所需的最小总硬币数。

输入格式

The first line contains a single integer tt (1≤t≤10001 \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 (1≤n≤2⋅1051 \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 S1S2…SnS_1S_2 \ldots S_n. ($S_i = $ 0 or $S_i = $ 1 for each 1≤i≤n1 \le i \le n)

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

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示字符串 SS 的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 SS,记作 S1S2…SnS_1S_2 \ldots S_n(对每个 1≤i≤n1 \le i \le n,均有 Si=0S_i = 0 或 Si=1S_i = 1)。

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

输出格式

For each test case, output the minimum total number of coins required to sort SS in increasing order.

对于每个测试用例,输出将 SS 按升序排序所需的最少硬币总数。

输入输出样例

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

在第一个测试用例中,SS 已经是有序的。

在第二个测试用例中,只需对 l=1,r=2l = 1, r = 2 执行该操作。

在第三个测试用例中,只需对 l=1,r=2l = 1, r = 2 执行该操作。

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

首页