#K52477. Marble Rearrangement
Marble Rearrangement
Marble Rearrangement
You are given \(N\) marbles. Each marble is described by an integer id and a color (string). Your task is to rearrange the marbles so that all marbles of the same color are grouped together. Within each color group, the marbles must be sorted in non-decreasing order of their id. Moreover, the groups themselves should be ordered in lexicographical order of the color names.
Example:
Input: 7 3 red 1 blue 2 red 4 blue 5 blue 6 yellow 2 yellow</p>Output: 1 blue 4 blue 5 blue 2 red 3 red 2 yellow 6 yellow
Here, marbles with color blue come first, then red, followed by yellow.
inputFormat
The first line of input contains an integer \(N\), representing the total number of marbles. The next \(N\) lines each contain two tokens: an integer representing the marble's id and a string representing its color, separated by a space.
For example:
7 3 red 1 blue 2 red 4 blue 5 blue 6 yellow 2 yellow
outputFormat
Output the rearranged list of marbles. Each marble should be printed on its own line with its id and color separated by a space. The marbles must be grouped by color (in lexicographical order) and, within each group, sorted in non-decreasing order by id.
For example:
1 blue 4 blue 5 blue 2 red 3 red 2 yellow 6 yellow## sample
7
3 red
1 blue
2 red
4 blue
5 blue
6 yellow
2 yellow
1 blue
4 blue
5 blue
2 red
3 red
2 yellow
6 yellow
</p>