Skip to content

Commit 7d4402c

Browse files
authored
Merge pull request #594 from AlgorithmWithGod/lkhyun
[20250802] BOJ / G3 / 오등큰수 / 이강현
2 parents 6ff2919 + 754db3a commit 7d4402c

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
```

0 commit comments

Comments
 (0)