結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 箱
提出日時 2021-07-30 22:04:50
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 554 ms / 2,000 ms
コード長 1,860 bytes
コンパイル時間 16,271 ms
コンパイル使用メモリ 426,320 KB
実行使用メモリ 58,760 KB
最終ジャッジ日時 2023-10-14 04:54:57
合計ジャッジ時間 27,743 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 283 ms
52,672 KB
testcase_01 AC 285 ms
52,772 KB
testcase_02 AC 281 ms
52,748 KB
testcase_03 AC 281 ms
52,712 KB
testcase_04 AC 278 ms
52,804 KB
testcase_05 AC 278 ms
52,900 KB
testcase_06 AC 280 ms
52,772 KB
testcase_07 AC 281 ms
52,704 KB
testcase_08 AC 282 ms
52,692 KB
testcase_09 AC 280 ms
52,700 KB
testcase_10 AC 283 ms
52,768 KB
testcase_11 AC 284 ms
53,008 KB
testcase_12 AC 279 ms
52,788 KB
testcase_13 AC 281 ms
52,820 KB
testcase_14 AC 283 ms
52,972 KB
testcase_15 AC 285 ms
52,904 KB
testcase_16 AC 554 ms
58,580 KB
testcase_17 AC 548 ms
58,484 KB
testcase_18 AC 512 ms
56,480 KB
testcase_19 AC 512 ms
56,620 KB
testcase_20 AC 410 ms
56,436 KB
testcase_21 AC 413 ms
56,736 KB
testcase_22 AC 416 ms
56,608 KB
testcase_23 AC 339 ms
53,012 KB
testcase_24 AC 495 ms
58,760 KB
testcase_25 AC 427 ms
58,368 KB
testcase_26 AC 511 ms
56,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val k = next()
    val c = Array(9) { nextInt() }
    if (k.length > n) {
        println(-1)
        return
    }
    val s = "0".repeat(n - k.length) + k
    var last = -1
    val deque = ArrayDeque<Char>()
    fun output() {
        while (deque.isNotEmpty()) {
            print(deque.pollFirst())
        }
        for (j in 0 until 9) {
            print(('1' + j).toString().repeat(c[j]))
        }
        println()
        return
    }
    for (i in 0 until n) {
        last = i
        if (s[i] == '0') {
            output()
            return
        } else if (c[s[i] - '1'] > 0) {
            c[s[i] - '1']--
            deque.addLast(s[i])
            last++
        } else {
            for (j in s[i] - '1' + 1 until 9) {
                if (c[j] > 0) {
                    deque.add('1' + j)
                    c[j]--
                    output()
                    return
                }
            }
            break
        }
    }
    for (i in last - 1 downTo 0) {
        val d = deque.pollLast()
        c[d - '1']++
        for (j in d - '1' + 1 until 9) {
            if (c[j] > 0) {
                deque.add('1' + j)
                c[j]--
                output()
                return
            }
        }
    }
    println(-1)
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

// 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