結果

問題 No.2048 L(I+D)S
ユーザー 👑 箱
提出日時 2022-08-09 14:34:17
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 1,840 bytes
コンパイル時間 18,438 ms
コンパイル使用メモリ 446,612 KB
実行使用メモリ 59,804 KB
最終ジャッジ日時 2023-10-20 04:56:08
合計ジャッジ時間 20,329 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 319 ms
59,804 KB
testcase_01 AC 304 ms
56,388 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun lis(p: List<Int>): Int {
    val lst = mutableListOf<Int>()
    loop@for (v in p) {
        for (i in lst.indices) {
            if (lst[i] > v) {
                lst[i] = v
                continue@loop
            }
        }
        lst.add(v)
    }
    return lst.size
}

fun PrintWriter.solve() {
    val n = nextInt()
    val p = (1..n).toMutableList()
    var num = 0
    do {
        if (lis(p) + lis(p.reversed()) == n) {
            num++
        }
    } while (nextPermutation(p))
    println(num)
}

fun nextPermutation(lst: MutableList<Int>): Boolean {
    val n = lst.size
    for (i in n - 2 downTo 0) {
        if (lst[i] < lst[i + 1]) {
            for (j in n - 1 downTo i + 1) {
                if (lst[j] > lst[i]) {
                    lst[i] = lst[j].also { lst[j] = lst[i] }
                    val tmpArray = lst.takeLast(n - (i + 1)).sorted()
                    tmpArray.forEachIndexed { index, e ->
                        lst[i + 1 + index] = e
                    }
                    return true
                }
            }
        }
    }
    return false
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0