1659B - Bit Flipping - CodeForces Solution


constructive algorithms greedy strings

Please click on ads to support us..

Python Code:

import math
import random
import sys




p2D = lambda x: print(*x, sep="\n")


def II(): return int(sys.stdin.buffer.readline())


def MI(): return map(int, sys.stdin.buffer.readline().split())


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))


def LLI(rows_number): return [LI() for _ in range(rows_number)]


def BI(): return sys.stdin.buffer.readline().rstrip()


def SI(): return sys.stdin.buffer.readline().rstrip().decode()


def li(): return [int(i) for i in input().split()]


def lli(rows): return [li() for _ in range(rows)]


def si(): return input()


def ii(): return int(input())


def ins(): return input().split()



def drank(d, processing, da, rank):
    tmp = 10 ** 9
    if len(d[da]) == 1:
        return 1
    for di in d[da]:
        if processing[di - 1] == 0:
            processing[di - 1] = 1
            tmp = min(tmp, drank(d, processing, di, rank))
            processing[di - 1] = 0
    rank[da - 1] = tmp + 1
    return tmp + 1


def binary_search(n, a):
    l = len(a)
    low = 0
    high = l - 1
    while high >= low:
        mid = (high + low) // 2
        if a[mid] == n:
            return mid
        elif a[mid] > n:
            high = mid - 1
        else:
            low = mid + 1
    return -1





def gcd(a,b):
    if b==0:
        x = 1
        y = 0
        return x, y, a
    x, y, g = gcd(b, a%b)
    return y, x- (a//b)*y, g

def solve():
    n, k = LI()
    s = SI()
    cnts = [0] * n
    p = k%2
    for i in range(n):
        if s[i] == str(p) and k:
            cnts[i] += 1
            k-=1
    cnts[-1] += k
    return ''.join([str(((cnts[i] + p) % 2) ^ int(s[i])) for i in range(n)])+ '\n'+' '.join(list(map(str, cnts)))


def main():
            for _ in range(II()):
        sys.stdout.write(str(solve()) + "\n")
                                    
            



if __name__ == "__main__":
    main()

C++ Code:

#include <iostream>
#include <map>
#include<unordered_map>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include<utility>
#include<climits>
#include<stack>

#define ull unsigned long long
#define ll long long
//#define NUM 1000000001
//ull mod  = 998244353;
using namespace std;


void solve()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    vector<int> ans(n,0);
    int tmp = k;
    for(int i=0;i<n and tmp >0 ;i++)
    {
        if(k%2 == s[i] - '0')
        {
            ans[i] = 1;
            tmp--;
        }
    }
    ans[n-1] += tmp;
    for(int i=0;i<n;i++)
    {
        if((k-ans[i])%2 == 1)
        {
            if(s[i] == '1') s[i] = '0';
            else s[i] = '1';
        }
    }
    cout<<s<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;

}
int main()
{
    int t ;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations