結果
| 問題 |
No.2048 L(I+D)S
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-08-09 14:34:17 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,840 bytes |
| コンパイル時間 | 16,416 ms |
| コンパイル使用メモリ | 446,540 KB |
| 実行使用メモリ | 95,552 KB |
| 最終ジャッジ日時 | 2024-09-20 00:38:55 |
| 合計ジャッジ時間 | 18,656 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 TLE * 1 |
| other | -- * 16 |
ソースコード
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