#K55437. Minimum Time to Arrange Balloons
Minimum Time to Arrange Balloons
Minimum Time to Arrange Balloons
John has a row of n balloons, where the ith balloon is painted in a certain color represented by an integer. In one operation, John can change the grouping of one balloon by effectively splitting one occurrence from a group of consecutive identical colored balloons. The goal is to achieve an arrangement where every group of balloons of the same color has a size of 1 (i.e. each balloon is isolated).
You are given an integer n and an integer k where:
- n is the total number of balloons with \(1 \le n \le 10^5\),
- The color of each balloon is given as an integer \(a_i\) with \(1 \le a_i \le 10^9\),
- k is the maximum number of operations available where \(0 \le k \le 10^5\).
The minimum number of operations required to achieve the desired arrangement is defined as the number of operations needed to split all groups such that each group's size becomes 1. For a given color with frequency \(f\), it requires \(f-1\) operations to separate its balloons (since a single balloon requires no operation). Therefore, if there are multiple colors, the total required operations will be the sum of \(f-1\) for each color. However, if k is insufficient, you can only perform at most k operations.
Your task is to compute the minimum time (i.e. the number of operations actually performed) to achieve the desired arrangement. Formally, if the total operations required is \(R = \sum_{c}(f_c - 1)\) over all color groups, then the answer is \(\min(R, k)\).
Example:
Input: n = 6, k = 4, colors = [1, 2, 2, 3, 3, 3] Output: 3 Explanation: The frequencies are: for color 1: 1, color 2: 2, and color 3: 3. Thus, the total required operations would be \((1-1) + (2-1) + (3-1) = 3\). Since 3 \(\le\) 4, the answer is 3.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains two space-separated integers n and k.
- The second line contains n space-separated integers representing the colors of the balloons.
For example:
6 4 1 2 2 3 3 3
outputFormat
Output a single integer to stdout representing the minimum number of operations required.
For example:
3## sample
6 4
1 2 2 3 3 3
3