• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects

백준 - 1966 프린터 큐

06 Jun 2022

Reading time ~1 minute

문제

1966 프린터 큐

screencapture

답

kotlin code: 최초 코드

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: 정리한 코드

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)
}


baekjoonkotlin코틀린백준실버구현자료 구조시뮬레이션큐 Share Tweet +1