-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17299.java
More file actions
63 lines (56 loc) · 1.82 KB
/
Copy path17299.java
File metadata and controls
63 lines (56 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// https://www.acmicpc.net/problem/17299
// 오등큰수
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
private static int n, arr[], found[], answer[];
private static final int MAX_INPUT = 1000000;
public static void main(String[] args) throws IOException {
// input
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(reader.readLine());
arr = new int[n];
found = new int[MAX_INPUT+1]; // count array
StringTokenizer st = new StringTokenizer(reader.readLine());
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(st.nextToken());
found[arr[i]]++;
}
// initialize
answer = new int[n];
answer[n-1] = -1;
Stack<Integer> stack = new Stack<>();
stack.add(n-1);
int flag;
// solution
for(int i=n-2; i>=0; i--){
flag = 0;
//System.out.println("cur index: "+i+", stack: "+stack);
while(!stack.isEmpty()){
//print(i, stack.peek());
if(found[arr[i]] < found[arr[stack.peek()]]){
answer[i] = arr[stack.peek()];
stack.add(i);
flag = 1;
break;
}
else{
stack.pop();
}
}
if(flag == 0){
answer[i] = -1;
stack.add(i);
}
}
// output
StringBuilder sb = new StringBuilder();
for(int i=0; i<n; i++)
sb.append(answer[i]).append(' ');
System.out.println(sb);
}
private static void print(int a, int b){
System.out.println(" F("+a+")="+found[arr[a]]+", F("+b+")="+found[arr[b]]);
}
}