Mock CCC '22 Contest 1 J4 - Snowball Fight

View as PDF

Submit solution


Points: 5 (partial)
Time limit: 0.6s
Python 1.5s
Memory limit: 256M

Author:
Problem type

N computer science students are having a snowball fight!

Each student has a list of targets, initially having one known target.

For every round in the fight, each student in the order 1, 2, \dots, N will throw a snowball to the first student in their target list, then remove the student from the front of their list. If their list is empty, they will do nothing.

There may be multiple occurrences of the same student in a student's list of targets. If the target student is on the list multiple times, only the first occurrence should be removed.

When all people have finished throwing or doing nothing for the round, students that had snowballs thrown at them this round will add whoever threw a snowball at them to the end of their list, in increasing order. For example, if students 1 and 3 threw a snowball to student 2, student 2 would add students 1 and 3 to the end of their list, in that order.

This will be repeated for T rounds, after which all the students will stop.

Determine the last student each student threw a snowball to.

Constraints

For this problem, you will be required to pass all the samples in order to receive points. In addition, you must pass all previous subtasks to earn points for a specific subtask.

2 \le N \le 2 \times 10^3

1 \le T \le 2 \times 10^3

Subtask 1 [4/15]

2 \le N \le 500

1 \le T \le 500

Subtask 2 [11/15]

No additional constraints.

Input Specification

The first line will contain N and T, space-separated.

The second and last line will contain N space-separated integers between 1 and N inclusive, the known initial target of the i-th student. No student will target themselves.

Output Specification

Output N space-separated integers between 1 and N inclusive, with the i-th integer denoting the student that the i-th student threw their last snowball to.

Sample Input

4 3
4 1 1 2

Sample Output

3 1 1 2

Explanation

In the first round, student 1 throws a snowball to student 4, students 2 and 3 throw a snowball to student 1, and student 4 throws a snowball to student 2.

The new target list looks like

1: \{2, 3\}

2: \{4\}

3: \{\}

4: \{1\}

In the second round, student 1 throws a snowball to student 2, student 2 throws a snowball to student 4, student 3 does nothing, and student 4 throws a snowball to student 1.

The new target list looks like

1: \{3, 4\}

2: \{1\}

3: \{\}

4: \{2\}

In the third round, student 1 throws a snowball to student 3, student 2 throws a snowball to student 1, student 3 does nothing, and student 4 throws a snowball to student 2.


Comments

There are no comments at the moment.