Kotlin/Algorithm Problems

<백준> 24.05.23에 푼 문제들

re트 2024. 5. 23. 23:26
728x90

1. KCPC(Silver 2)

[백준]

https://www.acmicpc.net/problem/3758

[깃허브]

 

ForCodeKata/baekjoon 문제집/KCPC at main · heesoo-park/ForCodeKata

Contribute to heesoo-park/ForCodeKata development by creating an account on GitHub.

github.com

제출 결과

작성한 코드는 다음과 같다.

import java.io.*
import java.util.*
import kotlin.math.*

fun main(args: Array<String>) {
    val br = BufferedReader(InputStreamReader(System.`in`))
    
    val t = br.readLine().toInt()
    repeat(t) {
        // n : 팀의 개수
        // k : 문제의 개수
        // t : 당신 팀의 ID
        // m : 로그 엔트리의 개수
        val (n, k, t, m) = br.readLine().split(' ').map { it.toInt() }
        
        val table = Array(n + 1) { IntArray(k + 1) { 0 } }
        val submitCnt = Array(n + 1) { 0 }
        val lastSubmit = Array(n + 1) { 0 }
        for (l in 0 until m) {
            // i : 팀 ID
            // j : 문제 번호
            // s : 획득한 점수
            val (i, j, s) = br.readLine().split(' ').map { it.toInt() }
            table[i][j] = maxOf(table[i][j], s)
            submitCnt[i]++
            lastSubmit[i] = l
        }
        
        val targetScore = table[t].sum()
        var rank = 1
        for (l in 1..n) {
            if (l == t) continue
            
            if (table[l].sum() > targetScore) {
                rank++
            } else if (table[l].sum() == targetScore) {
                if (submitCnt[l] < submitCnt[t]) {
                    rank++
                } else if (submitCnt[l] == submitCnt[t]) {
                    if (lastSubmit[l] < lastSubmit[t]) {
                        rank++
                    }
                }
            } 
        }
        
        println(rank)
    }
}

 

2. 타노스(Silver 3)

[백준]

https://www.acmicpc.net/problem/20310

[깃허브]

 

ForCodeKata/baekjoon 문제집/타노스 at main · heesoo-park/ForCodeKata

Contribute to heesoo-park/ForCodeKata development by creating an account on GitHub.

github.com

제출 결과

작성한 코드는 다음과 같다.

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

fun main(args: Array<String>) {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    
    val s = br.readLine()
    var cntOfZero = s.count { it == '0' } / 2
    var cntOfOne = s.count { it == '1' } / 2
    
    val result = StringBuilder()
    for (i in s) {
        if (i == '1') {
            if (cntOfOne > 0) {
                cntOfOne--
            } else {
                result.append(i)
            }
        } else {
            if (cntOfZero > 0) {
                result.append(i)
                cntOfZero--
            }
        }
    }
    
    bw.write("$result")
    bw.flush()
    bw.close()
}

 

3. IF문 좀 대신 써줘(Silver 3)

[백준]

https://www.acmicpc.net/problem/19637

[깃허브]

 

ForCodeKata/baekjoon 문제집/IF문 좀 대신 써줘 at main · heesoo-park/ForCodeKata

Contribute to heesoo-park/ForCodeKata development by creating an account on GitHub.

github.com

제출 결과

작성한 코드는 다음과 같다.

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

fun main(args: Array<String>) {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    
    val title = mutableListOf<String>()
    val value = mutableListOf<Int>()
    val (n, m) = br.readLine().split(' ').map { it.toInt() }
    
    repeat(n) {
        val (t, v) = br.readLine().split(' ')
        title.add(t)
        value.add(v.toInt())
    }
    
    repeat(m) {
        val power = br.readLine().toInt()
        
        var low = 0
        var high = n - 1
        while (low <= high) {
            val mid = (low + high) / 2
            
            if (power > value[mid]) {
                low = mid + 1
            } else {
                high = mid - 1
            }
        }
        bw.write("${title[low]}\n")
    }
    
    bw.flush()
    bw.close()
}
반응형