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