File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class Main {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+ static int N ;
9+ static int [] buildings;
10+ static long ans = 0 ;
11+ public static void main (String [] args ) throws Exception {
12+ N = Integer . parseInt(br. readLine());
13+ buildings = new int [N ];
14+
15+ for (int i = 0 ; i < N ; i++ ) {
16+ buildings[i] = Integer . parseInt(br. readLine());
17+ }
18+
19+ ArrayDeque<Integer > key = new ArrayDeque<> ();
20+ ArrayDeque<Integer > value = new ArrayDeque<> ();
21+ key. push(0 );
22+ value. push(buildings[0 ]);
23+ for (int i = 1 ; i < N ; i++ ) {
24+ while (! key. isEmpty() && buildings[i] >= value. peek()){
25+ value. pop();
26+ ans += i - key. pop() - 1 ;
27+ }
28+ key. push(i);
29+ value. push(buildings[i]);
30+ }
31+ while (! key. isEmpty()){
32+ ans += N - key. pop() - 1 ;
33+ }
34+ bw. write(ans+ " " );
35+ bw. close();
36+ }
37+ }
38+ ```
You can’t perform that action at this time.
0 commit comments