-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathykm.java
More file actions
102 lines (88 loc) · 2.87 KB
/
ykm.java
File metadata and controls
102 lines (88 loc) · 2.87 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
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main{
static StringBuilder sb = new StringBuilder();
static int countNumInRow;
static class MidNumArray{
int totalCount; // 전체 숫자수
int midNum; // 중위값
int count; // 전체 숫자 개수
PriorityQueue<Integer> big = new PriorityQueue<>(); // 현재 중위값 보다 큰값 오름차순
PriorityQueue<Integer> small = new PriorityQueue<>(Comparator.reverseOrder());
public MidNumArray(int M){
sb.append((M+1)/2+"\n");
totalCount = M;
count = 0;
countNumInRow = 0;
}
public void addNum(int num){
if(count == 0){
midNum = num;
}else{
if(num>=midNum){
big.add(num);
}else{
small.add(num);
}
}
count++;
if(count%2 == 1){
findMidNum();
printMidNum();
}
if(count == totalCount){
sb.append("\n");
}
}
public void findMidNum(){
if(big.size() > small.size()){
small.add(midNum);
midNum = big.poll();
}else if(big.size() < small.size()){
big.add(midNum);
midNum = small.poll();
}
}
public void printMidNum(){
sb.append(midNum);
countNumInRow++;
if(countNumInRow>=10){
sb.append("\n");
countNumInRow = 0;
}else{
if(count < totalCount) {
sb.append(" ");
}
}
}
}
public static void main(String[] args) throws IOException{
System.setIn(new FileInputStream("input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
while(N-->0){
int M = Integer.parseInt(br.readLine());
MidNumArray array = new MidNumArray(M);
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 1 ; i<= M; i++){
int num = Integer.parseInt(st.nextToken());
array.addNum(num);
if(i%10 == 0){
st = new StringTokenizer(br.readLine());
}
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
}