結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 箱星箱星
提出日時 2021-07-30 22:04:50
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 599 ms / 2,000 ms
コード長 1,860 bytes
コンパイル時間 14,738 ms
コンパイル使用メモリ 445,048 KB
実行使用メモリ 76,704 KB
最終ジャッジ日時 2024-09-16 00:19:51
合計ジャッジ時間 26,655 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 300 ms
50,816 KB
testcase_01 AC 295 ms
50,688 KB
testcase_02 AC 296 ms
50,708 KB
testcase_03 AC 298 ms
50,960 KB
testcase_04 AC 300 ms
50,988 KB
testcase_05 AC 298 ms
50,736 KB
testcase_06 AC 300 ms
50,524 KB
testcase_07 AC 303 ms
50,968 KB
testcase_08 AC 301 ms
50,560 KB
testcase_09 AC 303 ms
50,668 KB
testcase_10 AC 302 ms
50,692 KB
testcase_11 AC 298 ms
50,740 KB
testcase_12 AC 301 ms
50,808 KB
testcase_13 AC 305 ms
50,588 KB
testcase_14 AC 305 ms
50,684 KB
testcase_15 AC 307 ms
50,800 KB
testcase_16 AC 593 ms
73,712 KB
testcase_17 AC 599 ms
75,704 KB
testcase_18 AC 541 ms
68,988 KB
testcase_19 AC 537 ms
68,992 KB
testcase_20 AC 435 ms
56,240 KB
testcase_21 AC 441 ms
56,480 KB
testcase_22 AC 438 ms
56,268 KB
testcase_23 AC 354 ms
52,488 KB
testcase_24 AC 533 ms
74,508 KB
testcase_25 AC 454 ms
76,704 KB
testcase_26 AC 528 ms
68,956 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