[BOJ] 1146 지그재그 서기 - P5

TaeGN·2024년 9월 25일

BOJ Platinum Challenge

목록 보기
96/114

문제풀이

  1. dp[현재 상태에서 나보다 키가 작은 학생 수][현재 상태에서 나보다 키가 큰 학생 수][다음에 키가 큰 학생이 나와야하는지 작은 학생이 나와야 하는지 여부]로 두고 dp테이블을 채워나간다.

주의사항

  1. N = 1일때를 고려해야 한다.

소요시간

25분


package 백준.Platinum.P5.p1146_지그재그서기

const val MOD = 1_000_000
fun main() {
    val N = readln().toInt()
    val dp = Array(N + 1) { Array(N + 1) { IntArray(2) } }
    for (i in 1..N) {
        for (j in 1..N) {
            if (i == j) continue
            val leftCount = if (i < j) j - 2 else j - 1
            val rightCount = if (i > j) N - j - 1 else N - j
            val type = if (i < j) 0 else 1
            dp[leftCount][rightCount][type]++
        }
    }
    for (remain in (N - 3) downTo 0) {
        for (leftCount in 0..remain) {
            val rightCount = remain - leftCount
            for (pLeftCount in (leftCount + 1)..(remain + 1)) {
                val pRightCount = remain + 1 - pLeftCount
                dp[leftCount][rightCount][1] = (dp[leftCount][rightCount][1] + dp[pLeftCount][pRightCount][0]) % MOD
            }
            for (pRightCount in (rightCount + 1)..(remain + 1)) {
                val pLeftCount = remain + 1 - pRightCount
                dp[leftCount][rightCount][0] = (dp[leftCount][rightCount][0] + dp[pLeftCount][pRightCount][1]) % MOD
            }
        }
    }

    println(if (N == 1) 1 else dp[0][0].sum() % MOD)
}

https://github.com/TaeGN/Algorithm/blob/master/src/%EB%B0%B1%EC%A4%80/Platinum/P5/p1146_%EC%A7%80%EA%B7%B8%EC%9E%AC%EA%B7%B8%EC%84%9C%EA%B8%B0/p1146_%EC%A7%80%EA%B7%B8%EC%9E%AC%EA%B7%B8%EC%84%9C%EA%B8%B0.kt


문제링크

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

0개의 댓글