Sunday, April 24, 2022

Flip to Invert

 JJ has a binary string 

S of length N. JJ can perform the following operation on S:

  • Select an i such that 1iN, and flip Si (i.e. change 0 to 1 and 1 to 0)

JJ wants to minimize the number of inversions in S by performing the above operation at most K times. Can you help JJ do so?

Recall that a pair of indices (i,j) in S is called an inversion if i<j and Si>Sj.

Input Format

  • The first line contains a single integer T - the number of test cases. Then the test cases follow.
  • The first line of each test case contains two integers N and K - the length of the binary string S and the maximum number of times JJ can perform the given operation.
  • The second line of each test case contains a binary string S of length N containing 0s and 1s only.

Output Format

For each test case, output the minimum number of inversions possible after performing the given operation at most K times.

Constraints

  • 1T105
  • 1N105
  • 0KN
  • Sum of N over all test cases does not exceed 2105

Sample Input 1 

3
8 3
00010111
5 1
10100
6 2
111000

Sample Output 1 

0
2
3

Explanation

Test case 1: We are allowed to perform at most 3 operations. We can perform the following operation on S00010_11100011111 which has 0 inversions. Therefore 0 is the answer.

Test case 2: We can perform the following operation on S1_010000100 which has 2 inversions. It can be proven that this is the minimum number of inversions possible.

Test case 3: We can perform the following operations on S11100_0111010_111011 which has 3 inversions. It can be proven that this is the minimum number of inversions possible.

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...