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

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.

题目类型:字符串

解题目标:通过特殊翻转,获得字典序上最大的字符串。

解题思路:

1.不难发现, k= 奇数时在’1‘上翻转,会保存本位的1,k =  偶数时,在’0‘上翻转。

2.’0‘上翻转本位翻一次,本位不变,剩余翻转量k-1, 变成奇数,则变成了与本身相反的,更大了。

3.而k = 奇数时, 可以通过翻转最后,使前面所有的翻转,继而k-1变成处理k为偶数的情况。

 4.翻完之后有剩余的,此时序列全都翻成了’1‘,则为了字典序最大,最优解是前n-1位都为’1‘,只变动最后一位,因此剩余次数全都放在最后一位,并且判断剩余次数是否为奇数,若为奇数,则最后一位必为’0‘(这是由于全’1‘情况, 最后一位为‘1’,翻转奇数次必与本身相反。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define int long long
#define PII pair<int, int>
#define rep(x, a, b) for(int x = a; x<=b; x++)
#define pre(x, a, b) for(int x = b; x>= a; x--)
const int N = 2e5+10;
int cnt[N];
using namespace std;
signed main()
{

 int t ;
  cin>>t;
  while(t -- )
  {
  	int  n, k;
  	cin>>n>>k;
  	string s ; 
  	cin>>s;
  	memset(cnt, 0, sizeof(cnt));
  	
	
  	if(k % 2 == 1)
  	{
  		rep(i, 0, s.length() - 1)
		{
			s[i] = (s[i] == '1' ? '0' : '1');
		}
	}
  		rep(i, 0, s.length() - 1)
  		{
  			if(s[i] == '0' && k> 0)
  			{
			  	cnt[i]++;
  				s[i] = '1';
  				k--;
			}
		}
		if(k > 0)
		{
			cnt[n - 1] += k;
			if(k % 2 == 1)s[s.length() - 1] = '0';
		}

	cout<<s<<endl;
	rep(i, 0 ,s.length() - 1) cout<<cnt[i]<<' ';
	cout<<endl;
} 



	return 0;
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值