Tokitsukaze and Good 01-String (hard version)

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 sisi to '0' or '1' (1≤i≤n1≤i≤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.

Input

The first contains a single positive integer tt (1≤t≤100001≤t≤10000) — the number of test cases.

For each test case, the first line contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — 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⋅105.

Output

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.

题意:

        给你一个01字符串,你可以将其各个位置替换为0/1使之每一段都为2的倍数;

        求最小替换步数以及替换完后的总段数。

样例:

输入:

5
10
1110011000
8
11001111
2
00
2
11
6
100110

 输出:

3 2
0 3
0 1
0 1
3 1

首先,我们可以将其看作2个2个一组,如果两个不一样,那么就需要一次更改;

如果两个相同,就证明一定有这样的一段0或者1,统计段数就是第二个答案;

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll T , n;
int main(){
    cin >> T;
    while(T --){
        string s;
        cin >> n >> s;
        ll ans1 = 0 , ans2 = 1;
        vector<ll>v;
        for(int i = 0 ; i < n - 1 ; i += 2){
            if(s[i] == s[i+1])v.push_back(s[i]);
            else ans1 ++;
        }
        int len = v.size();
        for(int i = 0 ; i < len - 1 ; i ++){
            if(v[i] != v[i+1])ans2 ++;
        }
        cout << ans1 << " " << ans2 << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值