가운데를 말해요
0.1 초 (하단 참고) | 128 MB | 54527 | 15939 | 11943 | 30.373% |
문제
백준이는 동생에게 "가운데를 말해요" 게임을 가르쳐주고 있다. 백준이가 정수를 하나씩 외칠때마다 동생은 지금까지 백준이가 말한 수 중에서 중간값을 말해야 한다. 만약, 그동안 백준이가 외친 수의 개수가 짝수개라면 중간에 있는 두 수 중에서 작은 수를 말해야 한다.
예를 들어 백준이가 동생에게 1, 5, 2, 10, -99, 7, 5를 순서대로 외쳤다고 하면, 동생은 1, 1, 2, 2, 2, 2, 5를 차례대로 말해야 한다. 백준이가 외치는 수가 주어졌을 때, 동생이 말해야 하는 수를 구하는 프로그램을 작성하시오.
입력
첫째 줄에는 백준이가 외치는 정수의 개수 N이 주어진다. N은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수이다. 그 다음 N줄에 걸쳐서 백준이가 외치는 정수가 차례대로 주어진다. 정수는 -10,000보다 크거나 같고, 10,000보다 작거나 같다.
출력
한 줄에 하나씩 N줄에 걸쳐 백준이의 동생이 말해야 하는 수를 순서대로 출력한다.
예제 입력 1 복사
7
1
5
2
10
-99
7
5
예제 출력 1 복사
1
1
2
2
2
2
5
알고리즘 분류
시간 제한
- Python 3: 0.6 초
- PyPy3: 0.6 초
- Python 2: 0.6 초
- PyPy2: 0.6 초
풀이
백준이가 정수 하나 외칠때마다 지금까지 백준이가 말한 수중 중간값을 말해야 한다.
짝수개라면 중간에 있는 두 수 중 가장 작은 수를 말해야 한다.
ex)
1 5 2 10 -99 7 5를 외치면
1 1 2 2 2 2 5를 차례대로 말해야 한다.
1 (1) -> 중간값 1
1, 5 (1 | 5)-> 짝수, 중간값 min 계산 1
1, 5, 2 (1 2 | 5)-> 중간값 2
1 5 2 10 (1 2 |5 10}-> 짝수 중간값 min 계산 2 5 2
1 5 2 10 -99 (-99 1 2 |5 10}-> 중간값 2
1 5 2 10 -99 7(-99 1 2 |5 7 10}-> 짝수 중간값 min 계산 2 5 2
1 5 2 10 -99 7 5(-99 1 2 |5 7 10}-> 짝수 중간값 min 계산 2 5 2
5 3 -1 -3 -5 2
5 (5) 중간값 5
5 3 (3 | 5) 중간값 3
5 3 -1 (-1 3 | 5) 중간값 3
5 3 -1 -3 (-3 -1 | 3 5) 중간값 -1
5 3 -1 -3 -5 (-5 -3 -1 | 3 5) 중간값 -1
5 3 -1 -3 -5 2(-5 -3 -1 | 2 3 5) 중간값 -1
돌아가는 형태를 보면
왼쪽을 left queue, 오른쪽을 rightqueue로 둔다.
입력 index가 홀수의 경우 왼쪽 queue로 추가해본다.
입력 index가 짝수의 경우 오른쪽 queue에 추가해본다.
왼쪽 queue에서 가장 큰 수랑 오른쪽 queue에서 가장 작은 수를 비교한다.
오른쪽이 왼쪽보다 작으면 2개 swap
왼쪽은 내림차순 오른쪽은 오른차순 정렬하면 된다.
그리고 항상 중간 값은 leftqueue의 마지막놈이 중간놈이 된다.
코드
package test1655;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collection;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int N; // 1~100000
static PriorityQueue<Integer> leftQueue;
static PriorityQueue<Integer> rightQueue;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
MyScanner sc = new MyScanner(System.in);
N = sc.nextInt();
leftQueue = new PriorityQueue<>(Comparator.reverseOrder());
rightQueue = new PriorityQueue<>();
for(int n = 1; n < N+1; n++) {
int number = sc.nextInt();
if(n %2 == 0) {
rightQueue.offer(number);
} else {
leftQueue.offer(number);
}
if(!leftQueue.isEmpty() && !rightQueue.isEmpty()) {
if(leftQueue.peek() > rightQueue.peek()) {
int temp = leftQueue.poll();
leftQueue.offer(rightQueue.poll());
rightQueue.offer(temp);
}
}
sc.write(leftQueue.peek() + "\n");
}
sc.close();
}
static class MyScanner {
final BufferedReader reader;
final BufferedWriter writer;
static StringTokenizer tokenizer = null;
MyScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
writer = new BufferedWriter(new OutputStreamWriter(System.out));
}
String nextToken() throws IOException {
if(tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
void write(String text) throws Exception{
writer.write(text);
}
void close() throws IOException {
reader.close();
writer.close();
}
}
}
'Algorithms > 2023 Pro 시험 준비' 카테고리의 다른 글
1854 K번째 최단경로 찾기 - 백준 (0) | 2023.08.01 |
---|---|
5419 북서풍 - 백준 (0) | 2023.07.31 |
1621 알고스팟 - 백준 (0) | 2023.07.24 |
2811 알약 - 백준 (0) | 2023.07.24 |
Pro 시험에서의 길찾기 알고리즘 (0) | 2023.07.19 |