(1, 3회차 시도 실패)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
PriorityQueue<Integer> small = new PriorityQueue<>();
PriorityQueue<Integer> big = new PriorityQueue<>(Collections.reverseOrder());
int n = Integer.parseInt(br.readLine());
for (int j = 0; j < n; j++) {
StringTokenizer st = new StringTokenizer(br.readLine());
char s = st.nextToken().charAt(0);
int num = Integer.parseInt(st.nextToken());
if(s == 'I'){
small.add(num);
big.add(num);
}else{
if(num == 1){
big.poll();
}else{
small.poll();
}
}
}
if(small.isEmpty() && big.isEmpty() || small.peek() == big.peek()){
bw.write("EMPTY\n");
}else if(small.isEmpty()){
bw.write(big.poll());
while(big.size() != 1){
big.poll();
}
bw.write(" " + big.poll()+"\n");
}else if(big.isEmpty()){
int tmp = small.poll();
while(small.size() != 1){
small.poll();
}
bw.write(small.poll() + " " + tmp+"\n");
}else{
bw.write(big.poll() + " " + small.poll() + "\n");
}
}
br.close();
bw.close();
}
}
1, 3회차는 비슷해서 1회차만 가져왔습니다
(2회차 시도 실패)
import java.io.*;
import java.util.*;
/**
* 풀이 과정:
* - 고민과 풀이:
*
*
* - 문제 해결:
*
* 시간복잡도: O()
* 공간복잡도: O()
*
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
PriorityQueue<Integer> small = new PriorityQueue<>();
PriorityQueue<Integer> big = new PriorityQueue<>(Collections.reverseOrder());
int n = Integer.parseInt(br.readLine());
for (int j = 0; j < n; j++) {
StringTokenizer st = new StringTokenizer(br.readLine());
char s = st.nextToken().charAt(0);
int num = Integer.parseInt(st.nextToken());
if(s == 'I'){
while(!big.isEmpty() && num > big.peek()){
small.add(big.poll());
}
big.add(num);
}else{
if(num == 1){
if(!big.isEmpty()){
big.poll();
}
}else{
if(!small.isEmpty()){
small.poll();
}
}
}
}
if(small.isEmpty() || big.isEmpty()){
bw.write("EMPTY\n");
}else{
bw.write(big.poll() + " " + small.poll() + "\n");
}
}
br.close();
bw.close();
}
}
(4회차 시도 성공)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
TreeMap<Integer, Integer> map = new TreeMap<>();
int n = Integer.parseInt(br.readLine());
for (int j = 0; j < n; j++) {
StringTokenizer st = new StringTokenizer(br.readLine());
char s = st.nextToken().charAt(0);
int num = Integer.parseInt(st.nextToken());
if(s == 'I'){
map.put(num, map.getOrDefault(num, 0) + 1);
}else{
if(map.isEmpty()){
continue;
}
if(num == 1){
if(map.put(map.lastKey(), map.get(map.lastKey())-1) == 1){
map.remove(map.lastKey());
}
}else{
if(map.put(map.firstKey(), map.get(map.firstKey())-1) == 1){
map.remove(map.firstKey());
}
}
}
}
if(map.isEmpty()){
bw.write("EMPTY\n");
}else{
bw.write(map.lastKey() + " " + map.firstKey()+"\n");
}
}
br.close();
bw.close();
}
}