-
Notifications
You must be signed in to change notification settings - Fork 2
/
21939번 - 문제 추천 시스템 Version 1.kt
72 lines (56 loc) · 2.09 KB
/
21939번 - 문제 추천 시스템 Version 1.kt
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
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
data class Problem(val number: Int, val level: Int)
val levelOf = IntArray(100_001) // 갖고 있는 문제의 난이도 - 나중에 풀었던 번호가 들어왔을 때 난이도를 구별하기 위함
val maxPQ = PriorityQueue(compareByDescending<Problem>{it.level}.thenByDescending{it.number})
val minPQ = PriorityQueue(compareBy<Problem>{it.level}.thenBy{it.number})
fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
val n = readLine().toInt()
repeat(n) {
val st = StringTokenizer(readLine())
val number = st.nextToken().toInt()
val level = st.nextToken().toInt()
val problem = Problem(number, level)
maxPQ.add(problem)
minPQ.add(problem)
levelOf[number] = level
}
val cmdArray = Array<String>(readLine().toInt()) {
readLine()
}
print(getAnswer(cmdArray))
}
fun getAnswer(cmdArray: Array<String>): StringBuilder {
val answer = StringBuilder()
cmdArray.forEach { cmd ->
val st = StringTokenizer(cmd)
when(st.nextToken()) {
"recommend" -> {
val pq =
if (st.nextToken().toInt() == 1) maxPQ
else minPQ
var problem = pq.peek()
// 이미 푼 문제면 큐에서 제거하고 다음 문제를 추천한다
while (levelOf[problem.number] != problem.level) {
pq.poll()
problem = pq.peek()
}
answer.append(problem.number).appendLine()
}
"add" -> {
val number = st.nextToken().toInt()
val level = st.nextToken().toInt()
val problem = Problem(number, level)
maxPQ.add(problem)
minPQ.add(problem)
levelOf[number] = level
}
"solved" -> {
val number = st.nextToken().toInt()
levelOf[number] = 0 // 문제 해결
}
}
}
return answer
}