Home 백준 - 1966 프린터 큐
Post
Cancel

백준 - 1966 프린터 큐

문제

1966 프린터 큐

screencapture

kotlin code: 최초 코드

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
fun main() {
    q1966()
}

fun q1966() {
    val testCaseCount = readln().toInt()
    val inputs = List<MutableList<Pair<Int, Boolean>>>(testCaseCount) { mutableListOf() }
    for(i in 0 until testCaseCount) {
        val targetIndex = readln().split(" ").last().toInt()
        val queue = readln().split(" ").mapIndexed { index, it -> Pair(it.toInt(), index == targetIndex) }
        inputs[i].addAll(queue)
    }

    for(input in inputs) {
        var printCount = 0
        while (true) {
            val max = input.maxOf { it.first }
            val cur = input.dequeue()
            if(cur.first == max) {
                printCount++
                if(cur.second) break
            }
            else {
                input.enqueue(cur)
            }
        }
        println(printCount)
    }
}

fun <E> MutableList<E>.dequeue(): E {
    val element = this.first()
    this.removeFirst()
    return element
}

fun <E> MutableList<E>.enqueue(element: E) {
    this.add(element)
}

kotlin code: 정리한 코드

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
fun main() {
    q1966()
}

fun q1966() {
    val testCaseCount = readln().toInt()
    val inputs = List(testCaseCount) { Printer() }
    val targetIndexes = mutableListOf<Int>()
    for (input in inputs) {
        targetIndexes.add(readln().split(" ").last().toInt())
        input.addAll(readln().split(" ").mapIndexed { index, it -> Prints(index, it.toInt()) })
    }

    for ((i, input) in inputs.withIndex()) {
        val targetIndex = targetIndexes[i]
        var printCount = 0
        while (input.isNotEmpty()) {
            val printResult = input.requestPrint()
            if (printResult.status != Printer.STATUS.COMPLETE) continue
            printCount++
            if (printResult.index != targetIndex) continue
            println(printCount)
            break
        }
    }
}

data class Prints(val index:Int, val importance:Int)
data class PrintResult(val status:Printer.STATUS, val index: Int)

class Printer {
    enum class STATUS {POSTPONE, COMPLETE}
    private val queue: MutableList<Prints> = mutableListOf()

    fun addAll(elements: List<Prints>) {
        this.queue.addAll(elements)
    }

    fun isNotEmpty(): Boolean = this.queue.isNotEmpty()

    fun requestPrint(): PrintResult {
        val highestImportance = queue.maxOf { it.importance }
        val target = queue.dequeue()
        return if(target.importance == highestImportance) print(target)
        else postpone(target)
    }

    private fun print(target: Prints): PrintResult = PrintResult(STATUS.COMPLETE, target.index)

    private fun postpone(target: Prints): PrintResult {
        queue.enqueue(target)
        return PrintResult(STATUS.POSTPONE, target.index)
    }

    private fun <E> MutableList<E>.dequeue(): E {
        val element = this.first()
        this.removeFirst()
        return element
    }

    private fun <E> MutableList<E>.enqueue(element: E) = this.add(element)
}
This post is licensed under CC BY 4.0 by the author.

백준 - 2740 행렬 곱셈

백준 - 11401 이항 계수 3 풀이