• 2022 ACM-ICPC 网络赛(1) A题


    A 01 Sequence

    Given a binary cyclic sequence S of length n, whose elements are either 0 or 1, you can do the following operation any number of times.

    • Operation: if the length of S is greater than or equal to 3, choose a position i (1≤i≤n) such that Si​=1, remove the element on position i and both its adjacent elements (remove 3 elements in total), and do not change the relative order of the other elements. Note that elements in the first and last positions are considered adjacent.

    A binary cyclic sequence S is called good if you can make S empty using the operation above.

    And the beauty of a binary cyclic sequence S, f(S), is defined as the minimum number of modifications needed to make S good. In a single modification you can flip an arbitrary element in S, that is, 0 becomes 1 and 1 becomes 0.

    Given are a binary string a of length n and q queries. For the i-th query you are given two integers li​ and ri​, and you should answer f(ali​..ri​​) (where we consider the substring ali​..ri​​ as a cyclic sequence).

    输入格式:

    The first line contains two integers n and q (3≤n≤10^6, 1≤q≤10^6) — the length of string a and the number of queries, respectively.

    The second line contains the string a1​a2​⋯an​, where each ai​ is either 0 or 1.

    Each of the following q lines contains two integers li​ and ri​ (1≤li​≤ri​≤n, (ri​−li​+1)≡0mod3) describing the i-th query.

    输出格式

    Output q lines, where the i-th line contains a single integer — the answer for the i-th query.

    输入样例:

    1. 7 7
    2. 1000011
    3. 1 3
    4. 2 4
    5. 3 5
    6. 4 6
    7. 5 7
    8. 1 6
    9. 2 7
    1. 12 1
    2. 110110110000
    3. 1 12
    1. 20 10
    2. 00001000100000010000
    3. 18 20
    4. 14 16
    5. 7 12
    6. 2 10
    7. 16 18
    8. 6 20
    9. 8 10
    10. 13 15
    11. 1 6
    12. 1 12

    输出样例:

    1. 0
    2. 1
    3. 1
    4. 0
    5. 0
    6. 1
    7. 1
    1
    
    1. 1
    2. 0
    3. 1
    4. 1
    5. 0
    6. 3
    7. 0
    8. 1
    9. 1
    10. 2

    代码长度限制

    16 KB

    时间限制

    2000 ms

    内存限制

    512 MB

     

    只要字符串中,不相邻的1的个数大于等于区间长度 / 3的话,就可以完全删除了.

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N=1e6+10;
    4. int f[N];
    5. int pre[N],last[N];
    6. int n,m;
    7. void solve()
    8. {
    9. cin>>n>>m;
    10. string s;
    11. cin >>s;
    12. s="?"+s;
    13. int cnt=0;
    14. for (int i=1;i<=n;i++)
    15. {
    16. if (s[i]=='1') cnt++;
    17. else cnt=0;
    18. f[i]=f[i-cnt-1]+cnt/2+cnt%2;
    19. pre[i]=cnt;
    20. }
    21. cnt=0;
    22. for(int i=n;i;i--)
    23. {
    24. if (s[i]=='1') cnt++;
    25. else cnt=0;
    26. last[i]=cnt;
    27. }
    28. while (m--)
    29. {
    30. int tmp = 0;
    31. int l, r;
    32. cin>>l>>r;
    33. tmp=pre[r]+last[l];
    34. int st=f[r-pre[r]]-f[l+last[l]]+tmp/2+tmp%2;
    35. if ((r-l+1)/3<=st) cout<<0<<"\n";
    36. else cout<<(r-l+1)/3-st<<"\n";
    37. }
    38. }
    39. int main()
    40. {
    41. ios::sync_with_stdio(false);
    42. cin.tie(0);
    43. cout.tie(0);
    44. solve();
    45. return 0;
    46. }

     

  • 相关阅读:
    Virtual Box 安装Ubuntu虚拟机以及配置
    JSP语法基础习题
    MySQL 常用函数 2022/09/06
    RSA非对称加密算法中的密钥对生成与传输
    【毕业设计】老人心率脉搏血压体征监测手表 - stm32 单片机 嵌入式 物联网
    PyTorch笔记 - Convolution卷积的原理 (2)
    有意识的神经网络之对比网络层和后意识层 两个em 自回归解码
    超级明星们的人物化身 NFT 将来到 The Sandbox 元宇宙
    C++的一些好用的限定修饰符
    这才是 SpringBoot 统一登录鉴权、异常处理、数据格式的正确打开姿势
  • 原文地址:https://blog.csdn.net/m0_61949623/article/details/126922946