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