Minimum Score
Practice
4.2 (9 votes)
Datastructures
Greedy algorithms
Basics of greedy algorithms
Algorithms
Problem
89% Success 1386 Attempts 30 Points 1.5s Time Limit 256MB Memory 1024 KB Max Code
You are given a binary array \(A\) with elements \(0\) and \(1\) of size \(N\), and integer \(K\). Let's call the score of an array the difference between the maximum and minimum element within the array. For example \([1,0,0,1,0,0]\), the Score of this given array is \(1-0 = 1\).
Now you have to partition the array into \(K\) continuous subarrays such that the summation of the score of all the subarrays is minimum. You also have to output starting and ending points of the subarrays corresponding to the minimum score, in increasing order of starting point. If there are multiple possibilities then output the subarrays such that starting points will be lexicographically smallest.
Input format
- The first line contains two space-separated integers \(N\), and \(K\) denoting the size of the array and the number of subarrays in which you have to divide the array respectively.
- The second line contains \(N\) space-separated integers denoting array \(A\).
Output format
- In the first line print the minimum score.
- Then print \(K\) lines, each line will contain starting and ending points of the subarray.
Constraints
- \( 2 \leq N \leq 10^6 \)
- \(1 \leq K \leq N\)
Submissions
Please login to view your submissions
Similar Problems
1.OrOasis
Points:30
5 votes
Tags:
Bit ManipulationAlgorithmsGreedy AlgorithmsBasics of Greedy Algorithms
Points:30
2 votes
Tags:
AlgorithmsGreedy AlgorithmsBasics of Greedy AlgorithmsGreedy algorithm
Points:30
6 votes
Tags:
Basics of Greedy AlgorithmsGreedy AlgorithmsAlgorithms
Editorial