-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain2.java
33 lines (26 loc) Β· 966 Bytes
/
Main2.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
package Stack.P2493;
import java.io.*;
import java.util.*;
public class Main2 {
static int N;
static int[] tops;
static Stack<Integer> s = new Stack<>();
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Stack/P2493/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
tops = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
tops[i] = Integer.parseInt(st.nextToken());
while (!s.isEmpty() && tops[s.peek()] < tops[i]) s.pop();
if (s.isEmpty()) bw.write("0 ");
else bw.write((s.peek()+1) + " ");
s.push(i);
}
bw.flush();
bw.close();
br.close();
}
}