-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
102 lines (87 loc) Β· 2.49 KB
/
Main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package Simulation.P1713;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int N, V;
static Vote[] candi;
static int cnt = 0;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Simulation/P1713/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
V = Integer.parseInt(br.readLine());
candi = new Vote[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < V; i++) {
int num = Integer.parseInt(st.nextToken());
int exist = find(num);
if (exist >= 0) {
candi[exist].count ++;
} else {
if (cnt < N) {
candi[cnt] = new Vote(num, i);
cnt ++;
} else {
candi[getMin()].init(num, i);
}
}
}
int[] ans = new int[N];
for (int i = 0; i < N && candi[i] != null; i++) {
ans[i] = candi[i].num;
}
Arrays.sort(ans);
for (int i = 0; i < N; i++) {
if (ans[i] == 0) continue;
System.out.print(ans[i] + " ");
}
System.out.println("");
}
static int find(int num) {
for (int i = 0; i < N && candi[i] != null; i++) {
if (candi[i].num == num) return i;
}
return -1;
}
static int getMin() {
int min = 1001;
int min_idx = -1;
for (int i = 0; i < N; i++) {
int count = candi[i].count;
if (count < min) {
min = count;
min_idx = i;
} else if (count == min && candi[i].old < candi[min_idx].old) {
min = count;
min_idx = i;
}
}
return min_idx;
}
}
class Vote {
int num;
int count;
int old;
public Vote(int num, int old) {
this.num = num;
this.count = 1;
this.old = old;
}
void init(int num, int old) {
this.num = num;
this.count = 1;
this.old = old;
}
@Override
public String toString() {
return "Vote{" +
"num=" + num +
", count=" + count +
", old=" + old +
'}';
}
}