CF1678B2.Tokitsukaze and Good 01-String (hard version)

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.

Tokitsukaze has a binary string ss of length nn, consisting only of zeros and ones, nn is even.

Now Tokitsukaze divides ss into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, ss is considered good if the lengths of all subsegments are even.

For example, if ss is "11001111", it will be divided into "11", "00" and "1111". Their lengths are 22, 22, 44 respectively, which are all even numbers, so "11001111" is good. Another example, if ss is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are 33, 22, 22, 33. Obviously, "1110011000" is not good.

Tokitsukaze wants to make ss good by changing the values of some positions in ss. Specifically, she can perform the operation any number of times: change the value of sis_i to '0' or '1' (1≤i≤n1 \leq i \leq n). Can you tell her the minimum number of operations to make ss good? Meanwhile, she also wants to know the minimum number of subsegments that ss can be divided into among all solutions with the minimum number of operations.

这是该问题的困难版本。两个版本的唯一区别在于,困难版本额外要求子段的最少数量。

Tokitsukaze 有一个长度为 nn 的二进制字符串 ss,仅由 '0' 和 '1' 构成,且 nn 为偶数。

现在,Tokitsukaze 将 ss 划分为最少数量的连续子段,使得每个子段内的所有比特均相同。此后,若所有子段的长度均为偶数,则称 ss 是“好的”。

例如,若 ss 为 "11001111",则它将被划分为 "11"、"00" 和 "1111"。它们的长度分别为 22、22、44,均为偶数,因此 "11001111" 是“好的”。另一个例子:若 ss 为 "1110011000",则它将被划分为 "111"、"00"、"11" 和 "000",其长度分别为 33、22、22、33。显然,"1110011000" 不是“好的”。

Tokitsukaze 希望通过对 ss 中某些位置的值进行修改,使其变为“好的”。具体而言,她可以执行任意次数的操作:将 sis_i(其中 1≤i≤n1 \leq i \leq n)的值改为 '0' 或 '1'。你能告诉她使 ss 变为“好的”所需的最少操作次数吗?同时,在所有达到最少操作次数的方案中,她还希望知道 ss 所能划分出的子段的最少数量。

输入格式

The first contains a single positive integer tt (1≤t≤10 0001 \leq t \leq 10\,000) — the number of test cases.

For each test case, the first line contains a single integer nn (2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5) — the length of ss, it is guaranteed that nn is even.

The second line contains a binary string ss of length nn, consisting only of zeros and ones.

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

第一行包含一个正整数 tt(1≤t≤10 0001 \leq t \leq 10\,000)—— 表示测试用例的数量。

对于每个测试用例,第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5)—— 表示字符串 ss 的长度,保证 nn 为偶数。

第二行包含一个长度为 nn 的二进制字符串 ss,仅由字符 0 和 1 组成。

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

输出格式

For each test case, print a single line with two integers — the minimum number of operations to make ss good, and the minimum number of subsegments that ss can be divided into among all solutions with the minimum number of operations.

对于每个测试用例,输出一行,包含两个整数:使 ss 变为“好串”所需的最少操作次数,以及在所有达到最少操作次数的方案中,ss 能被划分成的最小子段数量。

输入输出样例

  • 输入#1

    5
    10
    1110011000
    8
    11001111
    2
    00
    2
    11
    6
    100110

    输出#1

    3 2
    0 3
    0 1
    0 1
    3 1

说明/提示

In the first test case, one of the ways to make ss good is the following.

Change s3s_3, s6s_6 and s7s_7 to '0', after that ss becomes "1100000000", it can be divided into "11" and "00000000", which lengths are 22 and 88 respectively, the number of subsegments of it is 22. There are other ways to operate 33 times to make ss good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are 22, 44, 44 respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is 22.

In the second, third and fourth test cases, ss is good initially, so no operation is required.

在第一个测试用例中,使 ss 变为“好串”的一种方法如下:

将 s3s_3、s6s_6 和 s7s_7 修改为 '0',修改后 ss 变为 "1100000000",它可以被划分为 "11" 和 "00000000",其长度分别为 22 和 88,该串的子段数量为 22。还有其他仅需 33 次操作即可使 ss 变为“好串”的方式,例如 "1111110000"、"1100001100"、"1111001100",它们的子段数量分别为 22、44、44。容易发现,在所有使用最少操作次数的方案中,子段数量的最小值为 22。

在第二、第三和第四个测试用例中,ss 初始即为“好串”,因此无需任何操作。

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

首页