• B. Bit Flipping Codeforces Round #782 (Div. 2)


    You are given a binary string of length nn. You have exactly kk moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped (00 becomes 11, 11 becomes 00). You need to output the lexicographically largest string that you can get after using all kk moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.

    A binary string aa is lexicographically larger than a binary string bb of the same length, if and only if the following holds:

    • in the first position where aa and bb differ, the string aa contains a 11, and the string bb contains a 00.

    Input

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

    Each test case has two lines. The first line has two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105; 0≤k≤1090≤k≤109).

    The second line has a binary string of length nn, each character is either 00 or 11.

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

    Output

    For each test case, output two lines.

    The first line should contain the lexicographically largest string you can obtain.

    The second line should contain nn integers f1,f2,…,fnf1,f2,…,fn, where fifi is the number of times the ii-th bit is selected. The sum of all the integers must be equal to kk.

    Example

    input

    Copy

    6
    6 3
    100001
    6 4
    100011
    6 0
    000000
    6 1
    111001
    6 11
    101100
    6 12
    001110
    

    output

    Copy

    111110
    1 0 0 2 0 0 
    111110
    0 1 1 1 0 1 
    000000
    0 0 0 0 0 0 
    100110
    1 0 0 0 0 0 
    111111
    1 2 1 3 0 4 
    111110
    1 1 4 2 0 4

    思路:可以发现如果k是偶数的话所有的数都不用变,1还是1,0还是0,所以我们遇到0的时候需要消耗一次不变次数,让他的操作数转化成单数,然后就可以把0变成1了,当前面的都最优转换的时候,如果k还有剩余,那么就放在最后一个字符上,然后再算一下操作步数,如果是奇数那么就变,如果是偶数那么就不变。因此我们列举的时候遍历的是0~n-2,s[n-1]单独去讨论。当操作数k是奇数的话,1变成0,0变成1,所以我们需要阻止1变成0,那么我们就在遇到1的时候消耗一个操作次数,让总共操作数变成偶数,这样就能保持1还是1,当我们用完消耗次数的时候,以后的1就都得转换成0了。

    1. #include<iostream>
    2. #include<algorithm>
    3. #include<cstring>
    4. using namespace std;
    5. typedef long long ll;
    6. const int N=2e5+7;
    7. ll n,k;
    8. ll a[N];
    9. void sove(){
    10. string s;
    11. cin>>n>>k;
    12. cin>>s;
    13. memset(a,0,sizeof a);
    14. ll con=k;
    15. if(k==0){
    16. cout<<s<<endl;
    17. for(int i=0;i<n;i++){
    18. cout<<a[i]<<" ";
    19. }
    20. cout<<endl;
    21. return ;
    22. }
    23. if(con%2!=0){
    24. for(int i=0;i<n-1;i++){
    25. if(s[i]=='0'){
    26. s[i]='1';
    27. }else{
    28. if(k>0){
    29. a[i]=1;
    30. k--;
    31. }else{
    32. s[i]='0';
    33. }
    34. }
    35. }
    36. if(k>0){
    37. a[n-1]=k;
    38. if((con-k)%2!=0){
    39. if(s[n-1]=='0')s[n-1]='1';
    40. else s[n-1]='0';
    41. }
    42. }else {
    43. if(s[n-1]=='0')s[n-1]='1';
    44. else s[n-1]='0';
    45. }
    46. }else{
    47. for(int i=0;i<n-1;i++){
    48. if(s[i]=='0'&&k>0){
    49. k--;
    50. s[i]='1';
    51. a[i]=1;
    52. }
    53. }
    54. if(k>0){
    55. a[n-1]=k;
    56. if((con-k)%2!=0){
    57. if(s[n-1]=='0')s[n-1]='1';
    58. else s[n-1]='0';
    59. }
    60. }
    61. }
    62. cout<<s<<endl;
    63. for(int i=0;i<n;i++){
    64. cout<<a[i]<<" ";
    65. }
    66. cout<<endl;
    67. }
    68. int main(){
    69. ios::sync_with_stdio(false);
    70. cin.tie() ,cout.tie() ;
    71. int t;
    72. cin>>t;
    73. while(t--){
    74. sove();
    75. }
    76. return 0;
    77. }

  • 相关阅读:
    Blend for Visual Studio 概述
    JS之instanceof方法手写
    【校招VIP】[产品][985][5分]实习经历无法凸显个人能力
    Metabase学习教程:模型-1
    Oracle19c安装以及问题汇总
    申请专利必须把技术公开吗?
    DOM中Element对象常用属性与方法大全
    记录一次更新inter arc显卡驱动失败
    k8s集群-7 service
    redis做缓存(cache)
  • 原文地址:https://blog.csdn.net/qq_61903556/article/details/125461439