Dr. Anne Anderson Coding Contest 1 P3 - The Unwritten Rules

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

Ayman is on a school bus coming back from his AP Seminar field trip. His bus has N rows of seats, with each row being divided into two groups: seats on the left side and seats on the right side, with an empty central aisle in between for passengers to move. Each row has M seats on each side, with a total of 2M seats per row. The bus is completely full, and he numbers each student on the bus from 1 to 2NM, primarily from left to right and then from top to bottom.

When leaving the bus, there's a few unwritten rules that people should follow. Firstly, a person shouldn't leave the bus until everyone from all rows ahead of them have already left, on both the left and right sides. Secondly, a person shouldn't leave until everyone else closer to the aisle on their side of the bus (left or right) has already left, so that their path to the aisle doesn't go through other students.

As Ayman waits to leave, he realizes he can make a competitive programming problem out of this! Given the seating of the bus and the order that students leave, can you determine if all students followed the unwritten rules?

Constraints

22NM106

Subtask 1 [30%]

M=1

Subtask 2 [30%]

N=1

Subtask 3 [40%]

No additional constraints.

Input Specification

The first line of output contains the integers N and M, separated by a space.

The next 2NM lines of output will contain the order the students will leave the bus.

Output Specification

The output will consist of one line: yes if the entire bus followed the unwritten rules, or no if one or more passengers violated the rules.

Sample Input 1

Copy
2 2
2
1
3
4
7
6
5
8

Sample Output 1

Copy
yes

Explanation for Sample 1

The students are numbered in the bus as follows:

Copy
1 2   3 4
5 6   7 8

Student 2 is the closest to the aisle on the left side of the bus, so they can leave. Now that student 2 has left, student 1 is the closest to the aisle on the left side of the bus, so they can leave. Student 3 is the closest to the aisle on the right side of the bus, so they can leave. Now that student 3 has left the bus, student 4 is the closest to the aisle on the right side of the bus, so they can leave.

Student 7 is the closest to the aisle on the right side, so they can leave. Student 6 is the closest to the aisle on the left side, so they can leave. Student 5 is now the closest to the aisle on the left side, so they can leave. Student 8 is now the closest to the aisle on the right side, so they can leave.

Sample Input 2

Copy
2 1
1
3
2
4

Sample Output 2

Copy
no

Explanation for Sample 2

This case satisfies the conditions of Subtask 1. The students are numbered in the bus as follows:

Copy
1   2
3   4

Student 3 is in row 2, but leaves before student 2 who is in row 1, violating an unspoken rule.

Sample Input 3

Copy
1 3
3
2
5
1
4
6

Sample Output 3

Copy
no

Explanation for Sample 3

This case satisfies the conditions of Subtask 2. The students are numbered in the bus as follows:

Copy
1 2 3   4 5 6

Student 5 leaves before student 4, even though student 4 is closer to the aisle than them on the right side of the bus, violating an unspoken rule.


Comments

There are no comments at the moment.