https://www.acmicpc.net/problem/7662
트리셋으로 처음에 시도했다가 중복된 값 처리 때문에 포기
우선순위큐 2개로 시도했다가 예외처리에서 막혀서 포기했다. 근데 이걸로 푼 사람이 있음
나는 트리셋으로 해결할 방법이 떠올라서 이걸로 통과하였다
import java.io.*;
import java.util.*;
class Data implements Comparable<Data>
{
public int value;
public int count;
public Data(int value, int count) {
this.value = value;
this.count = count;
}
@Override
public int compareTo(Data o) {
int ret = 0;
if(this.value < o.value)
ret = -1;
else if(this.value > o.value)
ret = 1;
return ret;
}
public Data set(int value, int count) {
this.value = value;
this.count = count;
return this;
}
}
public class Main {
public static void main(String[] args)
{
FastReader fr = new FastReader();
int T = Integer.parseInt(fr.nextLine());
int K = 0;
//TreeSet
TreeSet<Data> ts = new TreeSet<>();
Data tmp = new Data(0, 0);
for(int i = 0; i < T; ++i)
{
K = Integer.parseInt(fr.nextLine());
ts.clear();
for (int j = 0; j < K; ++j)
{
String[] operation = fr.nextLine().split(" ");
if((operation[0].equals("D")) && (ts.isEmpty()==false) )
{
if(operation[1].equals("-1"))
{
ts.first().count--;
if(ts.first().count == 0)
ts.remove(ts.first());
}
else if(operation[1].equals("1"))
{
ts.last().count--;
if(ts.last().count == 0)
ts.remove(ts.last());
}
}
else if(operation[0].equals("I"))
{
if(ts.contains(tmp.set(Integer.parseInt(operation[1]), 0)))
{
Data find = ts.floor(tmp.set(Integer.parseInt(operation[1]),0));
ts.remove(find);
ts.add(new Data(Integer.parseInt(operation[1]) ,find.count+1));
}
else
{
ts.add(new Data(Integer.parseInt(operation[1]), 1));
}
}
}
if(ts.isEmpty())
System.out.println("EMPTY");
else {
System.out.println(ts.last().value + " " + ts.first().value);
}
}
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int nextInt()
{
return Integer.parseInt(next());
}
}
오랜만에 다시 풀었다 TreeMap으로 풀었다... 2시간이나 걸렸다 하..
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));
int T = Integer.parseInt(br.readLine());
TreeMap<Integer, Integer> tmap = new TreeMap<>();
String[] input;
for (int i = 0; i < T; i++) {
int Q = Integer.parseInt(br.readLine());
for (int j = 0; j < Q; j++) {
input = br.readLine().split(" ");
String com = input[0];
int val = Integer.parseInt(input[1]);
if(com.equals("I")){
tmap.put(val, tmap.getOrDefault(val, 0)+1);
}else if(tmap.isEmpty() == false){
int key = 0;
if(val == -1){
key = tmap.firstKey();
}else{
key = tmap.lastKey();
}
val = tmap.get(key)-1;
if(val <= 0)
tmap.remove(key);
else
tmap.put(key, val);
}
}
if(tmap.isEmpty())
System.out.println("EMPTY");
else{
System.out.println(tmap.lastKey() + " " + tmap.firstKey());
}
tmap.clear();
}
}
}