-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreetParade.java
82 lines (76 loc) · 2.54 KB
/
StreetParade.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* https://www.spoj.com/problems/STPAR/ #queue #stack #ad-hoc-1 use queue for main street. use stact
* for side street
*/
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
public class StreetParade {
static String calSub(int[] arr) {
Deque<Integer> mainStreet = new LinkedList<Integer>();
Deque<Integer> sideStreet = new LinkedList<Integer>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1) {
mainStreet.add(arr[i]);
} else if (!mainStreet.isEmpty()) {
while (!sideStreet.isEmpty() && sideStreet.peek() == mainStreet.peek() + 1) {
mainStreet.add(sideStreet.pop());
}
if (!sideStreet.isEmpty() && arr[i] > sideStreet.peek()) {
return "no";
}
if (arr[i] == mainStreet.peek() + 1) {
mainStreet.add(arr[i]);
} else {
sideStreet.push(arr[i]);
}
} else {
if (!sideStreet.isEmpty() && sideStreet.peek() < arr[i]) {
return "no";
}
sideStreet.push(arr[i]);
}
}
if (!sideStreet.isEmpty() && sideStreet.peek() < mainStreet.peek()) {
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();
}
}
/**
* import java.util.Scanner; import java.util.Deque; import java.util.LinkedList;
*
* <p>class StreetParade { static String calSub(int[] arr) { Deque<Integer> sideStreet = new
* LinkedList<Integer>(); //stack int lastCar = 0;
*
* <p>for (int i=0; i < arr.length; i++) { 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]); } } if (!sideStreet.isEmpty() && sideStreet.peekLast() < lastCar) {
* return "no"; }
*
* <p>return "yes"; }
*
* <p>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(); } }
*/