KotlinAlgorithm#10 (BOJ1966)

박채빈·2021년 10월 22일
0

KotlinAlgorithm

목록 보기
10/28
post-thumbnail

BOJ1966 프린터 큐

링크

코드

import java.io.*
import java.util.*

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))

    val input = br.readLine().toInt()

    repeat(input) {
        var cnt = 0
        val (N, M) = br.readLine().split(" ").map { it.toInt() }
        val docs = br.readLine().split(" ")

        val docQueue: Queue<Pair<Int, Int>> = LinkedList()

        for((idx, doc) in docs.withIndex()) {
            docQueue.offer(Pair(idx, doc.toInt()))
        }

        while(true) {
            val max = docQueue.maxOf { it.second }
            val tmp = docQueue.poll()

            if(tmp.second == max) {
                cnt++
                if(tmp.first == M) {
                    bw.write("$cnt\n")
                    break
                }
                else    continue
            }
            else {
                docQueue.offer(tmp)
            }
        }
    }

    bw.flush()
    bw.close()
    br.close()
}
profile
안드로이드 개발자

0개의 댓글