-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_2108.java
More file actions
89 lines (75 loc) ยท 2.01 KB
/
_2108.java
File metadata and controls
89 lines (75 loc) ยท 2.01 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
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
package backjoon;
// https://www.acmicpc.net/problem/2108
// ํต๊ณํ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _2108 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//sol memory 40392 runtime 264
int N = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
// ์
๋ ฅ๊ฐ์ ๋ฒ์ : -4000 ~ 4000
int[] arr = new int[8001];
int sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
// median ๊ณผ mode ๋ -4000~4000 ์ ์ ์ธํ ์๋ก ์ด๊ธฐํํ๋ฉด ๋๋ค.
int median = 0;
int mode = 0;
for(int i = 0; i < N; i++) {
int value = Integer.parseInt(br.readLine());
sum += value;
arr[value + 4000]++;
if(max < value) {
max = value;
}
if(min > value) {
min = value;
}
}
int count = 0; // ์ค์๊ฐ ๋น๋ ๋์ ์
int mode_max = 0; // ์ต๋น๊ฐ์ ์ต๋๊ฐ
// ์ด์ ์ ๋์ผํ ์ต๋น๊ฐ์ด 1๋ฒ๋ง ๋ฑ์ฅํ์๊ฒฝ์ฐ true, ์๋๊ฒฝ์ฐ false
boolean flag = false;
for(int i = min + 4000; i <= max + 4000; i++) {
if(arr[i] > 0) {
// <์ค์๊ฐ ์ฐพ๊ธฐ
if(count < (N + 1) / 2) {
count += arr[i]; // i๊ฐ์ ๋น๋์๋ฅผ count ์ ๋์
median = i - 4000;
}
// ์ต๋น๊ฐ ์ฐพ๊ธฐ
if(mode_max < arr[i]) {
mode_max = arr[i];
mode = i - 4000;
flag = true; // ์ฒซ ๋ฑ์ฅ์ด๋ฏ๋ก true ๋ก ๋ณ๊ฒฝ
}
// ์ด์ ์ต๋น๊ฐ ์ต๋๊ฐ๊ณผ ๋์ผํ ๊ฒฝ์ฐ๋ฉด์ ํ ๋ฒ๋ง ์ค๋ณต๋๋ ๊ฒฝ์ฐ
else if(mode_max == arr[i] && flag == true) {
mode = i - 4000;
flag = false;
}
}
}
System.out.println((int)Math.round((double)sum / N));
System.out.println(median);
System.out.println(mode);
System.out.println(max - min);
}
}
/*
input
5
1
3
8
-2
2
output
2
2
1
10
*/