Reorganization
InterviewTime limit2sMemory limit512 MB
Given each employee's rank in ID order, decide whether a binary hierarchy exists where every non-root has a supervisor with a smaller ID and a better rank.
- Level
Medium6 of 10
- Topics
- Greedy, Tree, Stack, Implementation
- Solved
- No attempts yet
Problem
Alice and Bob run a company together, and they want to reorganize it into a single hierarchy.
There are employees. Each employee has a distinct employee ID from to , and a distinct integer rank ; a smaller rank value means a higher rank.
Decide whether the company can be organized so that all of the following hold:
- Exactly one employee is the director, who has no supervisor.
- Every employee except the director has exactly one direct supervisor, whose employee ID is smaller and whose rank is higher (that is, whose rank value is smaller).
- Each employee directly supervises at most people.
Input
The first line contains the number of employees ().
Each of the next lines contains one integer: the -th of these lines gives the rank () of the employee whose ID is . All rank values are distinct.
Output
Print YES if the company can be reorganized as required, and NO otherwise.