結果

問題 No.1279 Array Battle
ユーザー 👑 箱
提出日時 2021-10-04 23:05:34
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 675 ms / 2,000 ms
コード長 1,861 bytes
コンパイル時間 17,655 ms
コンパイル使用メモリ 430,740 KB
実行使用メモリ 65,012 KB
最終ジャッジ日時 2023-09-30 05:45:49
合計ジャッジ時間 27,752 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 300 ms
55,172 KB
testcase_01 AC 344 ms
58,904 KB
testcase_02 AC 320 ms
58,984 KB
testcase_03 AC 300 ms
53,680 KB
testcase_04 AC 297 ms
52,952 KB
testcase_05 AC 296 ms
53,440 KB
testcase_06 AC 320 ms
57,072 KB
testcase_07 AC 315 ms
59,024 KB
testcase_08 AC 301 ms
53,444 KB
testcase_09 AC 317 ms
56,956 KB
testcase_10 AC 322 ms
57,244 KB
testcase_11 AC 330 ms
57,452 KB
testcase_12 AC 459 ms
64,544 KB
testcase_13 AC 548 ms
63,300 KB
testcase_14 AC 548 ms
63,124 KB
testcase_15 AC 644 ms
62,728 KB
testcase_16 AC 645 ms
65,012 KB
testcase_17 AC 675 ms
64,596 KB
testcase_18 AC 667 ms
64,756 KB
testcase_19 AC 668 ms
63,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun nextPermutation(array: MutableList<Int>): Boolean {
    var tmp: Int
    for (i in array.size - 2 downTo 0) {
        if (array[i] < array[i + 1]) {
            for (j in array.size - 1 downTo i + 1) {
                if (array[j] > array[i]) {
                    tmp = array[i]
                    array[i] = array[j]
                    array[j] = tmp

                    val tmpArray = array.takeLast(array.size - (i + 1)).sorted()
                    tmpArray.forEachIndexed { index, e ->
                        array[i + 1 + index] = e
                    }

                    return true
                }
            }
        }
    }
    return false
}

fun PrintWriter.solve() {
    val n = nextInt()
    val a = Array(n) { nextInt() }
    val b = Array(n) { nextInt() }
    val p = (0 until n).toMutableList()
    val map = mutableMapOf<Int, Int>()
    do {
        var point = 0
        for (i in 0 until n) {
            point += maxOf(0, a[p[i]] - b[i])
        }
        map[point] = (map[point] ?: 0) + 1
    } while (nextPermutation(p))
    val max = map.keys.maxOrNull()!!
    println(map[max]!!)
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", 1.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