-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3015.java
More file actions
42 lines (37 loc) · 1.17 KB
/
Copy path3015.java
File metadata and controls
42 lines (37 loc) · 1.17 KB
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
// https://www.acmicpc.net/problem/3015
// 오아시스 재결합
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
Stack<Group> stack = new Stack<>();
long answer = 0;
for(int i=0; i<n; i++){
long height = Long.parseLong(reader.readLine());
while(!stack.isEmpty() && stack.peek().height < height)
answer += stack.pop().count;
if(stack.isEmpty())
stack.push(new Group(height, 1));
else if(stack.peek().height == height){
answer += stack.peek().count++;
if(stack.size() > 1)
answer++;
}
else if(stack.peek().height > height){
answer++;
stack.push(new Group(height, 1));
}
}
System.out.println(answer);
}
}
class Group{
long height, count;
public Group(long h, long c){
height = h;
count = c;
}
}