結果

問題 No.873 バイナリ、ヤバいなり!w
ユーザー バカらっくバカらっく
提出日時 2019-09-05 02:40:55
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 1,972 bytes
コンパイル時間 17,269 ms
コンパイル使用メモリ 429,452 KB
実行使用メモリ 76,236 KB
最終ジャッジ日時 2023-09-06 12:02:22
合計ジャッジ時間 22,431 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 WA -
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 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:6:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^
Main.kt:52:9: warning: variable 'list' is never used
    val list = mutableListOf<Array<Int>>()
        ^
Main.kt:60:21: warning: name shadowed: list
                val list = mutableListOf(idx)
                    ^

ソースコード

diff #

import java.lang.Math.sqrt
import java.lang.StringBuilder
import java.util.*
import kotlin.Comparator

fun main(args: Array<String>) {
    var n = readLine()!!.toInt()
    var prevChar = '0'
    var list = PriorityQueue<Int>(Comparator { a: Int, b: Int ->  com(a, b, prevChar == '0') })
        getList(n)!!.forEach { list.add(it) }
    val ans = StringBuilder()
    while (list.isNotEmpty()) {
        val num = list.peek()
        val word = getWord(prevChar == '0', num)
        ans.append(word)
        prevChar = word.last()
        list.poll()
    }
    println(ans.toString())
}

fun com(a:Int, b:Int, prevZero: Boolean):Int {
    var mul = -1
    if(prevZero) {
        mul = 1
    }
    if(a % 2 == 1) {
        if(b % 2 == 1) {
            return a.compareTo(b) * mul
        } else {
           return -1 * mul
        }
    } else {
        if(b % 2 == 0) {
            return b.compareTo(a) * mul
        } else {
            return 1 * mul
        }
    }
}

val map = mutableMapOf<Int, Array<Int>>()
fun getList(num:Int):Array<Int>? {
    if(num == 1) {
        return arrayOf(1)
    }
    if(num == 0) {
        return arrayOf()
    }
    map[num]?.let { return it }
    val lastIndex = sqrt(num.toDouble()).toInt()
    val list = mutableListOf<Array<Int>>()
    var len = num
    var minList = Array<Int>(0, {0})
    (1..lastIndex).reversed().forEach {
        val ret = getList(num - it * it)
        val idx = it
        ret?.let {
            if(it.size < len) {
                val list = mutableListOf(idx)
                list.addAll(it)
                len = it.size
                minList = list.toTypedArray()
            }
        }
    }
    if(minList.isEmpty()) {
        return null
    }
    map[num] = minList
    return minList
}

fun getWord(prevZero:Boolean, num:Int):String {
    if(prevZero) {
        return (1..num).map { (it - 1) % 2 }.joinToString("")
    } else {
        return (1..num).map { it % 2 }.joinToString("")
    }
}
0