Hide

Problem B
Dorm Laundry Sorting

/problems/dormlaundry/file/statement/en/img-0001.jpg
Photo by Alex Tyson on Unsplash

At this residential hall, there is only one working washing machine this week. Each student signs up for a time slot represented by an integer, the hour at which they start their laundry. Unfortunately, multiple students may sign up for the same hour. When this happens, the residential advisor resolves the conflict as follows:

  • The student whose name comes first alphabetically keeps that hour.

  • All other students originally assigned to that hour are moved to the next hour in a 24-hour cycle.

  • If a student is moved from hour $t$, they are assigned to hour $t+1$; in particular, if $t = 23$, then the next hour is $0$ on the next day, in which will not interfere with the $0$ slot on the first day, if it was taken.

  • If the next hour already contains one or more students, the same rule is applied again at that hour.

This process continues until every hour has at most one student. Your task is to compute the final schedule after all conflicts have been resolved.

Input

The first line of input contains a single integer $n$ ($1 \leq n \leq 24$), the number of sign-ups. Each of the next $n$ lines contains a student’s first name (a non-empty string of between $1$ and $50$ lowercase English letters) followed by a space and an integer $t$ ($0 \leq t < 24$), the hour at which the student initially signed up.

Output

After all conflicts have been resolved, output $n$ lines. Each line must contain the student’s name and their assigned hour, separated by a space.

The lines must be printed in the chronological order in which the final assignments occur during conflict resolution, where time is considered to continue across day boundaries. In particular, if one student is assigned to hour $23$ and another is assigned to hour $0$ because they were pushed past hour $23$, then the hour $0$ assignment occurs later and must be printed after the hour $23$ assignment.

Sample Input 1 Sample Output 1
3
alice 9
bob 9
carol 9
alice 9
bob 10
carol 11
Sample Input 2 Sample Output 2
4
alice 9
bob 9
carol 10
dave 10
alice 9
bob 10
carol 11
dave 12

Please log in to submit a solution to this problem

Log in