LCC '21 Contest 5 J5 - Marble Groupings

View as PDF

Submit solution

Points: 7
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type

Mabel has a line of N marbles, each of which is one of M colours.

She can swap the position of two adjacent marbles, with the goal of creating a "complete colour grouping". A complete colour grouping for a colour c occurs when all marbles of colour c are arranged contiguously in a block of consecutive marbles.

Help Mabel find the minimum number of swaps to reach a complete colour grouping for each colour.

Input Specification

The first line will contain two space-separated integers, N (1 \leq N \leq 10^5) and M (1 \leq M \leq N), the number of marbles and the number of colours.

The second line will contain N space-separated integers, a_i (1 \leq a_i \leq M), representing the colour of the i^{th} of marble.

Output Specification

Output one line containing M space-separated integers, representing the minimum number of swaps required to reach a complete colour grouping for each of the M colours.

Sample Input

8 3
3 1 3 2 2 1 3 2

Sample Output

3 2 4

Sample Explanation

For colour 1, a complete colour grouping can be achieved in the following state: 3 1 1 3 2 2 3 2. This takes 3 swaps to reach.

For colour 2, a complete colour grouping can be achieved in the following state: 3 1 3 2 2 2 1 3. This takes 2 swaps to reach.

For colour 3, a complete colour grouping can be achieved in the following state: 1 3 3 3 2 2 1 2. This takes 4 swaps to reach.


Comments

There are no comments at the moment.