File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed
Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.Stack ;
4+
5+ public class BJ_3015_ 오아시스_재결합 {
6+
7+ private static final BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ private static final BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
9+
10+ private static int N ;
11+ private static long ans;
12+
13+ private static Stack<Node > s;
14+
15+ private static class Node {
16+ int val;
17+ int cnt;
18+
19+ Node (int val , int cnt ) {
20+ this . val = val;
21+ this . cnt = cnt;
22+ }
23+ }
24+
25+ public static void main (String [] args ) throws IOException {
26+ sol();
27+ }
28+
29+ private static void sol () throws IOException {
30+ N = Integer . parseInt(br. readLine());
31+ s = new Stack<> ();
32+
33+ ans = 0 ;
34+ for (int i = 0 ; i < N ; i++ ) {
35+ int n = Integer . parseInt(br. readLine());
36+ Node cur = new Node (n, 1 );
37+
38+ while (! s. isEmpty() && s. peek(). val <= cur. val) {
39+ Node prev = s. pop();
40+
41+ ans += prev. cnt;
42+ if (prev. val == cur. val) {
43+ cur. cnt += prev. cnt;
44+ }
45+ }
46+ if (! s. isEmpty()) {
47+ ans++ ;
48+ }
49+ s. push(cur);
50+ }
51+ bw. write(ans + " \n " );
52+ bw. flush();
53+ bw. close();
54+ br. close();
55+ }
56+
57+ }
58+ ```
You can’t perform that action at this time.
0 commit comments