• A. Balance the Bits--Codeforces Round #712 (Div. 1)


    Problem - 1503A - Codeforces

    A. Balance the Bits

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not.

    You are given a binary string ss of length nn. Construct two balanced bracket sequences aa and bb of length nn such that for all 1≤i≤n1≤i≤n:

    • if si=1si=1, then ai=biai=bi
    • if si=0si=0, then ai≠biai≠bi

    If it is impossible, you should report about it.

    Input

    The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

    The first line of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105, nn is even).

    The next line contains a string ss of length nn, consisting of characters 0 and 1.

    The sum of nn across all test cases does not exceed 2⋅1052⋅105.

    Output

    If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower).

    If the answer is "YES", output the balanced bracket sequences aa and bb satisfying the conditions on the next two lines.

    If there are multiple solutions, you may print any.

    Example

    input

    Copy

    3
    6
    101101
    10
    1001101101
    4
    1100
    

    output

    Copy

    YES
    ()()()
    ((()))
    YES
    ()()((()))
    (())()()()
    NO
    

    Note

    In the first test case, a=a="()()()" and b=b="((()))". The characters are equal in positions 11, 33, 44, and 66, which are the exact same positions where si=1si=1.

    In the second test case, a=a="()()((()))" and b=b="(())()()()". The characters are equal in positions 11, 44, 55, 77, 88, 1010, which are the exact same positions where si=1si=1.

    In the third test case, there is no solution.

    ==================================================================================================================================================

    C题很多套路是,1特殊到一般,2排除不可能情况之后的构造。

    本题是第二种

    首先0就代表了上下必须要有一个位置是右括号),显然出现在开头可以直接跳过,同理结尾也是如此

    然后考虑奇偶性,1的话上下一样,而我们左括号必须是总长度一半,如果1是奇数,那么0必定是奇数

    这样总长度才是偶数,而1是奇数时,左右括号一定是一偶一奇,同理0里面也是1偶一奇

    假设 1里面左偶右奇  那么上下1都必定是左偶右奇,而0里面的一奇一偶,只可能满足一方的,反转之后就无法满足令一方。故1必定是偶数。

    然后我们考虑构造,数据范围可以看出构造一定是线性的。这个只能靠感性猜测,我们猜先填1会比较方便,一半左一半右

    1      1                 1          1

    (   (               )             )

    然后我们猜测0的位置

    1     0  1   0      1       1

    (    (  (     )      )   )

    1     0   1        1    0   1

    ( (   )    (  )   )

    可以看出0其实是连续翻转着填的

    总之此类题目不要想太多深奥结论,就从最简单的特殊情况,奇偶性,考虑简单线性递推即可。结果总会意外巧合。

    1. # include<iostream>
    2. using namespace std;
    3. char ans[200000+10];
    4. int main ()
    5. {
    6. int t;
    7. cin>>t;
    8. while(t--)
    9. {
    10. int len;
    11. cin>>len;
    12. string s;
    13. cin>>s;
    14. if(s[0]=='0'||s[s.length()-1]=='0')
    15. {
    16. cout<<"NO"<<endl;
    17. continue;
    18. }
    19. int cnt0=0,cnt1=0;
    20. for(int i=0;i<s.length();i++)
    21. {
    22. if(s[i]=='0')
    23. cnt0++;
    24. else
    25. cnt1++;
    26. }
    27. if(cnt0%2)
    28. {
    29. cout<<"NO"<<endl;
    30. continue;
    31. }
    32. int cnt00=0,cnt11=0;
    33. for(int i=0;i<s.length();i++)
    34. {
    35. if(s[i]=='1')
    36. {
    37. cnt11++;
    38. if(cnt11<=cnt1/2)
    39. {
    40. ans[i]='(';
    41. }
    42. else
    43. {
    44. ans[i]=')';
    45. }
    46. }
    47. else
    48. {
    49. cnt00++;
    50. if(cnt00&1)
    51. {
    52. ans[i]='(';
    53. }
    54. else
    55. {
    56. ans[i]=')';
    57. }
    58. }
    59. }
    60. cout<<"YES"<<endl;
    61. for(int i=0;i<s.length();i++)
    62. {
    63. cout<<ans[i];
    64. }
    65. cout<<endl;
    66. for(int i=0;i<s.length();i++)
    67. {
    68. if(s[i]=='0')
    69. {
    70. if(ans[i]=='(')
    71. {
    72. cout<<')';
    73. }
    74. else
    75. {
    76. cout<<'(';
    77. }
    78. }
    79. else
    80. {
    81. cout<<ans[i];
    82. }
    83. }
    84. cout<<endl;
    85. }
    86. return 0;
    87. }

  • 相关阅读:
    【面试刷题】——C++四种类型转化
    基于 Habana Gaudi 的 Transformers 入门
    vue3中的route和router
    【开发篇】七、RedisTemplate与StringRedisTemplate + Jedis与Lettcus
    吴恩达机器学习-1
    nexus创建Maven私服图文教程
    我的创作纪念日 | 软件测试成长之路
    浅谈代码数据安全
    SystemC入门学习-第3章 数据类型
    Grafana配置对接Prometheus并配置Dashboard
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/125440897