C. Get the Long Binary Number time limit per test1 second memory limit per test256…
- User
C. Get the Long Binary Number time limit per test1 second memory limit per test256 megabytes Consider a binary number x .Define c0(x) as the number of 0 in x ,c1(x) as the number of 1 in x .
For example,x=10100 ,we can get c0(x)=3,c1(x)=2 .
Now you're given a binary number y (without leading zero) of length n and an integer k .Your task is find the smallest binary number x (without leading zero),which satisfies:
y≤x ; c0(x)−c1(x)=k . Input The first line of input will contain a single integer t(1≤t≤105) , denoting the number of test cases.
Each test case consists of two line of input.
The first line contains two integers n,k(1≤n≤105,−105≤k≤105) .
The second line contains a binary number y (without leading zero) of length n .
The sum of n,|k| over all test cases will not exceed 4∗105 .
Output For each test case,output on a new line — the smallest binary number x (without leading zero),which satisfies:
y≤x ; c0(x)−c1(x)=k . Example InputСкопировать 5 1 -1 0 6 -4 110100 6 1 100000 7 1 1110000 10 -2 1011010111 OutputСкопировать 1 110111 1000011 1110000 1011011001
solve this task using this solution #include <bits/stdc++.h> using namespace std;
string f(int m, int k){ while(true){ if( (m - k) % 2 == 0 ){ int c1 = (m - k) / 2; if(c1 >=1 && c1 <= m){ string x = "1"; c1--; if(m - c1 -1 > 0){ x += string(m - c1 -1, '0'); } x += string(c1, '1'); return x; } } m++; } }
void solve(){ int n, k; cin >> n >> k; string s; cin >> s; string res = f(n, k); \here u need to try to construct number >= then s from res with the same digits as in res if(res >= s) cout << res << "\n"; else cout << f(n+1, k) << "\n"; }
int main(){ int t; cin >> t; while(t--) { solve(); } return 0; }
- Assistant