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

Problem - 1659B - Codeforces

B. Bit Flipping

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Note

Here is the explanation for the first testcase. Each step shows how the binary string changes in a move.

  • Choose bit 11: 1–00001→1–111101_00001→1_11110.
  • Choose bit 44: 1111–10→0001–011111_10→0001_01.
  • Choose bit 44: 0001–01→1111–100001_01→1111_10.

The final string is 111110111110 and this is the lexicographically largest string we can get.

首先k次是必须分配完的,为了我们的答案最优我们从左开始分配,先让左边的达到最优。以k为奇数为例,如果当前位置为1,那么我们必须分配一下1次转换,然后抵消掉1次,使得本位置遭受的被动转换为1次;本次转换必须用在本位置,用在其他位置都只会导致本位置遭受奇数次演变变成0.如果当前是0,我们无需转换,因为其必定会受到其他位置被动转换奇数次转换成1。之所以只分配1次或者0次,是因为我们贪心使得尽可能的靠前位置兼顾的同时,后面的位置得以延续。

另外就是k剩余时,全部放在最后一个位置是最优的,这个可以分奇偶进行讨论,但比较麻烦,可以凭感性理解。一旦放在最后一个位置也能转化成1,那么我们便将整个数列都变成了1,一旦放在最后一个位置会让其转化成0,那么如果我们想让他也是1,会改变之前位置的奇偶性,导致更小。

#include<bits/stdc++.h>
using namespace std;

int ans[200000+10];

int main ()
{


    int t;

    cin>>t;

    while(t--)
    {

        int len,k;

        cin>>len>>k;

        string s;

        cin>>s;


        int kk=k;

        memset(ans,0,sizeof(ans));

        if(k%2==0)
        {


        for(int i=0;i<len;i++)
        {
            if(!k)
                break;

            if(s[i]=='0')
            {

                ans[i]=1;
                k--;

            }

        }

        }
        else
        {
            for(int i=0;i<len;i++)
        {
            if(!k)
                break;

            if(s[i]=='1')
            {

                ans[i]=1;

                k--;

            }

        }

        }

        ans[len-1]+=k;

       for(int i=0;i<len;i++)
       {
           if((kk-ans[i])%2==1)
           {
               if(s[i]=='1')
                cout<<'0';
               else
                cout<<'1';

           }
           else
           {

               if(s[i]=='1')
                cout<<'1';
               else
                cout<<'0';

           }
       }
       cout<<endl;


        for(int i=0;i<len;i++)
        {
            cout<<ans[i]<<" ";

        }

        cout<<endl;

    }
    return 0;

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值