CF1693F.I Might Be Wrong
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:256MB
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.
给你一个长度为 n 的二进制字符串 S,其下标从 1 到 n。你可以执行以下操作任意次(可以为零次):
-
选择两个整数 l 和 r(满足 1≤l≤r≤n)。令 cnt0 表示子串 S[l…r] 中字符
0出现的次数,cnt1 表示子串 S[l…r] 中字符1出现的次数。你可以花费 ∣cnt0−cnt1∣+1 枚硬币,将子串 S[l…r] 升序排序。(这里 S[l…r] 表示字符串 S 中从位置 l 开始、到位置 r 结束的子串)例如,若 $S = $
11001,我们可以在子串 S[2…4] 上执行该操作,花费 ∣2−1∣+1=2 枚硬币,得到新字符串 $S = $10011。
求将 S 升序排序所需的最小总硬币数。
输入格式
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.
第一行包含一个整数 t(1≤t≤1000)—— 表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 表示字符串 S 的长度。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 S,记作 S1S2…Sn(对每个 1≤i≤n,均有 Si=0 或 Si=1)。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, output the minimum total number of coins required to sort S in increasing order.
对于每个测试用例,输出将 S 按升序排序所需的最少硬币总数。
输入输出样例
输入#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.
在第一个测试用例中,S 已经是有序的。
在第二个测试用例中,只需对 l=1,r=2 执行该操作。
在第三个测试用例中,只需对 l=1,r=2 执行该操作。
输入解题思路,AI测评打分。不知道怎么写?