-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
63 lines (58 loc) · 1.48 KB
/
Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* https://www.spoj.com/problems/STPAR/
* #queue #stack #ad-hoc-1 #todo
*/
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
class Test {
static String calSub(int[] arr) {
Deque<Integer> sideStreet = new LinkedList<Integer>(); // stack
int lastCar = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1) {
// mainStreet.add(arr[i]);
lastCar = 1;
} else if (lastCar != 0) {
while (!sideStreet.isEmpty() && sideStreet.peekLast() == lastCar + 1) {
sideStreet.pollLast();
lastCar++;
}
if (!sideStreet.isEmpty() && arr[i] > sideStreet.peekLast()) {
return "no";
}
if (arr[i] == lastCar + 1) {
lastCar++;
} else {
sideStreet.addLast(arr[i]);
}
} else {
if (!sideStreet.isEmpty() && sideStreet.peekLast() < arr[i]) {
return "no";
}
sideStreet.addLast(arr[i]);
}
}
if (!sideStreet.isEmpty() && sideStreet.peekLast() < lastCar) {
return "no";
}
return "yes";
}
static void calculate() {
Scanner sc = new Scanner(System.in);
while (true) {
int n = sc.nextInt();
if (n == 0) {
break;
}
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
arr[i] = temp;
}
System.out.println(calSub(arr));
}
}
public static void main(String[] args) {
calculate();
}
}