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.
Example
input
Copy
5 10 1110011000 8 11001111 2 00 2 11 6 100110
output
Copy
3 2 0 3 0 1 0 1 3 1
Note
In the first test case, one of the ways to make ss good is the following.
Change s3s3, s6s6 and s7s7 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.
思路:题目让在改变次数最少的情况下将每段都符合是偶数个相同,在此基础上,问最少的子段是多少个,我们可以知道只要 i 是偶数个遍历即可,因为从头开始都要求是偶数个相同,2将是最小的偶数(0不算),只要满足2的也能满足更大的偶数,所以我们偶数个遍历即可(也就是2)。在次基础上开始hard版本,开始考虑最少有几段,当我们遇到不同的时候我们只会有两种选法,一种是当前位置和前一位置相等,另一种是当前位置和后一位置相同,所以这些位置可以随意改成全0或全1,不会让答案增加(因为和左端或者右端合并了,无非是使左端或者右端增长了而已),所以我们只要将不需要修改的地方连接起来,看它们能组合成多少个不同的字符串即可 。
完整代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
const int N=2e5+10;
int a[N];
char b[N];
void solve()
{
vector<char>v;
string s;
int n;
cin>>n;
cin>>s;
int ans=0;
int idx=0;
for(int i=0;i<n;i+=2)
{
if(s[i]==s[i+1]) v.push_back(s[i]),v.push_back(s[i+1]);
else ans++;
}
int len=v.size();
int cnt=1;
for(int i=0;i<len-1;i++)
{
if(v[i]!=v[i+1])cnt++;
}
cout<<ans<<" "<<cnt<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}