-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
32 lines (24 loc) Β· 908 Bytes
/
Main.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
package Stack.P1863;
import java.io.*;
import java.util.*;
public class Main {
static int n, x, y, cnt;
static Stack<Integer> stack;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Stack/P1863/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
stack = new Stack<>();
while (n-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
while (!stack.isEmpty() && stack.peek() > y) {
stack.pop();
cnt ++;
}
if (y != 0 && (stack.isEmpty() || stack.peek() < y)) stack.push(y);
}
System.out.println(cnt + stack.size());
}
}