public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Deque<Integer> stack = new ArrayDeque<>();
int count = 0;
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
st.nextToken();
int y = Integer.parseInt(st.nextToken());
while (!stack.isEmpty() && stack.peek() > y) {
stack.pop();
count++;
}
if (stack.isEmpty() || stack.peek() != y) {
stack.push(y);
}
}
while (!stack.isEmpty()) {
if (stack.pop() > 0) {
count++;
}
}
System.out.println(count);
br.close();
}
출처:https://www.acmicpc.net/problem/1863