[BOJ 6791] Reorganization

View as PDF

Submit solution

Points: 3
Time limit: 2.0s
Memory limit: 512M

Problem types
Allowed languages
Assembly, Awk, C, C++, Java, Pascal, Perl, Python, Sed, Text

Alice and Bob own a huge company. This company was losing money consistently over the last 30 years, since its owners spent too much time playing games with mathematicians. Alice and Bob decide to make a change.</p>

Alice and Bob start by giving unique employee IDs to each of the n employees (1 ≤ n ≤ 100, 000), where each ID I is in the range (1 ≤ I ≤ 100, 000).

Then, Alice and Bob give unique ranks to each employee. Each rank R is an integer such that 1 ≤ R ≤ 10, 000, 000. After this, they plan to reorganize the company, by making sure that the employees satisfy the following conditions:

  1. There is exactly one director, who has no supervisor;
  2. Except for the director, each employee has a supervisor, and this supervisor has a smaller employee ID and a higher rank (the value of rank is smaller); and
  3. Each employee can supervise at most 2 people.

Alice and Bob are eager to know whether their company can be reorganized successfully.

입력 형식

The input is a total of n + 1 lines. The first line contains n (1 ≤ n ≤ 100, 000), indicating the number of employees. On the next n lines are n distinct integers R (1 ≤ R ≤ 10, 000, 000), one integer per line, where the ith integer indicates the rank of the employee with ID i.

출력 형식

Output YES if the company can be reorganized successfully; output NO otherwise.

예제 입력 1

6
1
6
5
2
3
4

예제 출력 1

NO

예제 입력 2

6
1
6
2
3
4
5

예제 출력 2

YES

Comments

There are no comments at the moment.