DMOPC '22 Contest 1 P1 - Up-down Sequence

View as PDF

Submit solution

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

Author:
Problem type

An up-down sequence is any sequence where the elements alternate between increasing and decreasing. Formally, a sequence A of N integers is considered to be an up-down sequence if for each 1<i<N, either Ai1<Ai>Ai+1 or Ai1>Ai<Ai+1. Given a sequence of N integers, some of which are forgotten, you must determine whether it is possible to replace each forgotten element with any integer so that the resulting sequence is an up-down sequence. To ensure the integrity of your solution, there will be T test cases.

Constraints

1T106

3N106

0Ai109

The sum of N over all test cases does not exceed 106.

Subtask 1 [20%]

1Ai109

Subtask 2 [80%]

No additional constraints.

Input Specification

The first line contains an integer T, the number of test cases. The next 2T lines describe the test cases.

The first line of each test case contains a single integer N.

The second line of each test case contains N integers A1,A2,,AN. Forgotten elements are denoted by 0s in the sequence.

Output Specification

For each test case, output a single line containing YES if it is possible to make it an up-down sequence or NO otherwise.

Sample Input

Copy
4
7
1 0 2 5 3 0 2
5
3 0 1 2 3
4
1 0 0 1
3
6 6 0

Sample Output

Copy
YES
NO
YES
NO

Explanation for Sample

For the first test case, one may convert the sequence to [1,3,2,5,3,4,2].

For the third test case, one may convert the sequence to [1,1,2,1].


Comments

There are no comments at the moment.