DMOPC '18 Contest 1 P1 - Sorting

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 2.0s
Java 2.5s
Python 2.5s
Memory limit: 64M

Author:
Problem type

Roger has a list of N positive integers A1,A2,,AN. However, his list is not yet finalized. Some of these numbers are wildcards which will be represented as zeroes in the list. Roger will try to assign the wildcards a value so that

  • The list A is sorted from least to greatest
  • All wildcards have the same value, which is a positive integer

Help Roger find out if this is possible. Output YES if he can assign the wildcards a value so that A is sorted and NO otherwise.

Constraints

0Ai1000000 for all 1iN.
If Ai=0, then it is a wildcard, otherwise 1Ai1000000.

Subtask 1 [30%]

1N100

Subtask 2 [30%]

1N1000

Subtask 3 [40%]

1N200000

Input Specification

The first line contains a single integer N.
The next line contains N space-separated integers, A1,A2,,AN.

Output Specification

Output a single string: YES if it is possible and NO otherwise.

Sample Input 1

Copy
6
0 0 1 5 5 5

Sample Output 1

Copy
YES

Explanation for Sample Output 1

The first two elements are wildcards. Setting them to 1 gives a sorted list.

Sample Input 2

Copy
6
1 5 5 5 5 1

Sample Output 2

Copy
NO

Explanation for Sample 2

There are no wildcards, so Roger cannot do anything. A is not sorted so the answer is no.

Sample Input 3

Copy
6
1 0 2 0 3 0

Sample Output 3

Copy
NO

Comments

There are no comments at the moment.