File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-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 StringTokenizer st;
9+ static int N ;
10+ static int [] tops;
11+ static int [] reception;
12+
13+ public static void main (String [] args ) throws Exception {
14+ N = Integer . parseInt(br. readLine());
15+ st = new StringTokenizer (br. readLine());
16+ tops = new int [N + 1 ];
17+ reception = new int [N + 1 ];
18+
19+ for (int i = 1 ; i <= N ; i++ ) {
20+ tops[i] = Integer . parseInt(st. nextToken());
21+ }
22+
23+ ArrayDeque<int[]> stack = new ArrayDeque<> ();
24+ stack. push(new int []{tops[N ],N });
25+
26+ for (int i = N - 1 ; i >= 1 ; i-- ) {
27+ while (! stack. isEmpty() && tops[i] > stack. peek()[0 ]){
28+ int [] top = stack. pop();
29+ reception[top[1 ]] = i;
30+ }
31+ stack. push(new int []{tops[i],i});
32+ }
33+
34+ StringBuilder sb = new StringBuilder ();
35+ for (int i = 1 ; i <= N ; i++ ) {
36+ sb. append(reception[i]);
37+ if (i != N ){
38+ sb. append(" " );
39+ }
40+ }
41+ bw. write(sb. toString());
42+ bw. close();
43+ }
44+ }
45+ ```
You can’t perform that action at this time.
0 commit comments