Binary Sum

5

3 votes
Basics of Greedy Algorithms, Greedy Algorithms, Algorithms, C++
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.
 

Problem statement

Given an array S of N length consisting of only binary numbers (0 or 1). We need to decompose S into K consecutive non-empty subarrays such that every element is present in only one subarray.

Say subarrays are S[1], S[2], S[3], .... , S[K]. Choose an array B , which is a permutation of 1 to K i.e. it is of size K and include every element from 1 to K.

Now , form a new array V = S[B[1]] + S[B[2]] + ......  + S[B[K]]

Suppose, X is equal to the count of subarrays in V such that sum of elements in the subarray is greater than the Floor(length of the subarray / 2).

Aim is to maximize X.

Input format :

  • First line contains two space-separated integers : N and K 
  • Second line contains N space-separated integers denoting array S.

Output format :

  • In the first K lines, print one integer denoting end index of i-th subarray (indexes are 1 based).
  • In next line print K space separated integers, denoting array B (permutation of 1 to K)
  • End index of subarray should be in increasing order.

Verdict and scoring :

  • Your score is equal to the sum of the values of all test files. The value of each test file is equal to the value of X that you found.

    If decomposition of array S is invalid, or array B is invalid you will receive a wrong answer verdict.

Constraints :

  • N=103
  • 50K102
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • S1 = [0, 1]
  • S2 = [0, 1]
  • V = S[B[1]] + S[B[2]] = S[2] + S[1] = [0, 1] + [0, 1] = [0, 1, 0, 1]
  • X = 3 , subarrays which satisfy the condition are [1], [1], [1, 0, 1]
Editor Image

?