-
Notifications
You must be signed in to change notification settings - Fork 0
/
2910(Hashmap).java
68 lines (58 loc) · 1.88 KB
/
2910(Hashmap).java
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
64
65
66
67
68
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Triplet implements Comparable<Triplet>{
int value;
int frequency;
int index;
public Triplet(int v, int f, int i) {
value = v;
frequency = f;
index = i;
}
@Override
public int compareTo(Triplet t) {
if (frequency == t.frequency) {
return index - t.index;
}
return t.frequency - frequency;
}
}
public class Horororo {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int N = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());
HashMap<Integer, Integer> frequency = new HashMap<>();
HashMap<Integer, Integer> location = new HashMap<>();
st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < N; i++){
int num = Integer.parseInt(st.nextToken());
if(!frequency.containsKey(num)){
frequency.put(num, 1);
}else{
frequency.put(num, frequency.get(num) + 1);
}
if(!location.containsKey(num)){
location.put(num, i);
}
}
ArrayList<Triplet> s = new ArrayList<>();
for (int v : frequency.keySet()) {
int f = frequency.get(v);
int i = location.get(v);
Triplet t = new Triplet(v, f, i);
s.add(t);
}
Collections.sort(s);
for (Triplet t : s) {
for (int i = 0; i < t.frequency; i++) {
sb.append(t.value).append(" ");
}
}
System.out.println(sb);
}
}