• B2. 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.

    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.

    思路:

      以二为步长,若两位不同,则替换成和前边一样的;

      若相同,则看是否可合并,若不可,则多算一段

    代码:

    1. void solve(){
    2. int n;
    3. cin>>n;
    4. string s;
    5. cin>>s;
    6. int ans=0,cnt=0;
    7. char l='3';
    8. for(int i=0;isize();i+=2){
    9. if(s[i]!=s[i+1])ans++;
    10. else{
    11. if(l!=s[i])cnt++,l=s[i];
    12. }
    13. }cout<' '<<max(cnt,1ll)<<'\n';
    14. }

  • 相关阅读:
    【使用python写代码 完成绘制词云】
    【王道】操作系统OS第五章I/O管理(五)
    python-argparse
    【信号去噪】基于硬阈值、软阈值、半软阈值、Maxmin阈值、Garrote阈值小波变换实现心音去噪附matlab代码
    8月20日计算机视觉理论学习笔记——神经网络与BP算法
    Java Persistence API (JPA) 之 EntityManager
    word统计全部字符数。
    [ALI-签约代扣] 小程序环境下的签约代扣
    Vue3 复制 copy 功能实现(vue-clipboard3)
    基于深度学习的高精度工人阶梯检测识别系统(PyTorch+Pyside6+YOLOv5模型)
  • 原文地址:https://blog.csdn.net/m0_63054077/article/details/124672534