Submission #54185405


Source Code Expand

import java.io.*
import java.util.*
import java.util.function.IntPredicate
import kotlin.collections.*
import kotlin.system.exitProcess

val INPUT: InputStream = System.`in`
val OUTPUT: PrintStream = System.out

val _reader = INPUT.bufferedReader()

var _tokenizer: StringTokenizer = StringTokenizer("")
fun read(): String {
    while (!_tokenizer.hasMoreTokens()) {
        _tokenizer = StringTokenizer(_reader.readLine() ?: return "", " ")
    }
    return _tokenizer.nextToken()
}

fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readInts(n: Int) = List(n) { readInt() }
fun readIntArray(n: Int) = IntArray(n) { readInt() }
fun readDoubles(n: Int) = List(n) { readDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { readDouble() }
fun readLongs(n: Int) = List(n) { readLong() }
fun readLongArray(n: Int) = LongArray(n) { readLong() }

val _writer = PrintWriter(OUTPUT, false)
inline fun output(block: PrintWriter.() -> Unit) {
    _writer.apply(block).flush()
}

class MutableIntMap<K> : LinkedHashMap<K, Int>() {
    override operator fun get(key: K): Int = super.get(key) ?: 0

    fun increment(key: K, value: Int = 1) {
        this[key] = this[key] + value
    }

    companion object {
        fun <K> mutableIntMapOf(vararg pairs: Pair<K, Int>) =
            MutableIntMap<K>().apply { putAll(pairs) }
    }
}

typealias Pii = Pair<Int, Int>

fun readPii() = readInt() to readInt()
fun readPiis(n: Int) = List(n) { readPii() }

infix fun Int.hasBit(i: Int): Boolean = this and (1 shl i) > 0
infix fun Int.xorBit(i: Int): Int = this xor (1 shl i)
infix fun Long.hasBit(i: Int): Boolean = this and (1L shl i) > 0
infix fun Long.xorBit(i: Int): Long = this xor (1L shl i)

fun largerStackSize(stackSizeMegaBytes: Int = 100, action: () -> Unit) {
    Thread(null, action, "", 1024L * 1024 * stackSizeMegaBytes).apply {
        start()
        join()
    }
}

val LOCAL = System.getProperty("ONLINE_JUDGE") == null

// #################################################################################################

val A = mutableListOf(0, -1, 10, 1)

fun compare(i: Int, j: Int): Boolean {
    println("? $i $j")
    if (LOCAL) return A[i] < A[j]
    val ans = readInt()
    if (ans == -1) {
        exitProcess(1)
    }
    return ans == 1
}

fun mergeSort(a: List<Int>): List<Int> {
    if (a.size <= 1) return a
    val x = mergeSort(a.subList(0, a.size / 2))
    val y = mergeSort(a.subList(a.size / 2, a.size))
    return buildList {
        var i = 0
        var j = 0
        while (i < x.size || j < y.size) {
            if (i < x.size && (j == y.size || compare(x[i], y[j]))) add(x[i++])
            else add(y[j++])
        }
    }
}

fun sort(n: Int): List<Int> {
    fun dfs(a: List<Int>): List<Int> {
        if (a.isEmpty()) return a
        val p = a.random()
        val l = mutableListOf<Int>()
        val r = mutableListOf<Int>()
        for (i in a) if (i != p) {
            if (compare(i, p)) {
                l += i
            } else {
                r += i
            }
        }
        return dfs(l) + p + dfs(r)
    }
    return mergeSort((1..n).toList())
}

fun addition(i: Int, j: Int): Int {
    println("+ $i $j")
    if (LOCAL) {
        A += A[i] + A[j]
        return A.lastIndex
    }
    val ans = readInt()
    if (ans == -1) {
        exitProcess(1)
    }
    return ans
}

/** The upper bound of [low, high], which `predicate.test(it)` is true. */
fun upperBound(low: Int, high: Int, predicate: IntPredicate): Int? {
    if (low > high) return null
    var l = low
    var h = high
    while (l < h) {
        val m = (l + h + 1) / 2
        if (predicate.test(m)) {
            l = m
        } else {
            h = m - 1
        }
    }
    return l.takeIf(predicate::test)
}

fun sum(a: List<Int>): List<Int> {
    val b = a.subList(1, a.lastIndex).toMutableList()
    val c = addition(a.first(), a.last())
    val i = upperBound(0, b.lastIndex) {
        compare(b[it], c)
    }
    b.add((i ?: -1) + 1, c)
    return b
}

fun solve() {
    val n = readInt()
    var a = sort(n)
    while (a.size > 1) {
        a = sum(a)
    }
    println("!")
}

fun main() {
//    repeat(readInt()) { solve() }
    solve()
}

Submission Info

Submission Time
Task C - Beware of Overflow
User wangchaohui
Language Kotlin (Kotlin/JVM 1.8.20)
Score 500
Code Size 4443 Byte
Status AC
Exec Time 429 ms
Memory 68656 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 83
Set Name Test Cases
Sample 00-sample-001.txt
All 00-sample-001.txt, 01-handmade-001.txt, 01-handmade-002.txt, 01-handmade-003.txt, 01-handmade-004.txt, 01-handmade-005.txt, 01-handmade-006.txt, 01-handmade-007.txt, 01-handmade-008.txt, 01-handmade-009.txt, 01-handmade-010.txt, 01-handmade-011.txt, 01-handmade-012.txt, 01-handmade-013.txt, 01-handmade-014.txt, 01-handmade-015.txt, 01-handmade-016.txt, 01-handmade-017.txt, 01-handmade-018.txt, 01-handmade-019.txt, 01-handmade-020.txt, 01-handmade-021.txt, 01-handmade-022.txt, 01-handmade-023.txt, 01-handmade-024.txt, 01-handmade-025.txt, 01-handmade-026.txt, 01-handmade-027.txt, 02-random-001.txt, 02-random-002.txt, 02-random-003.txt, 02-random-004.txt, 02-random-005.txt, 02-random-006.txt, 02-random-007.txt, 02-random-008.txt, 02-random-009.txt, 02-random-010.txt, 02-random-011.txt, 02-random-012.txt, 02-random-013.txt, 02-random-014.txt, 02-random-015.txt, 02-random-016.txt, 02-random-017.txt, 02-random-018.txt, 02-random-019.txt, 02-random-020.txt, 02-random-021.txt, 02-random-022.txt, 02-random-023.txt, 02-random-024.txt, 02-random-025.txt, 02-random-026.txt, 02-random-027.txt, 02-random-028.txt, 02-random-029.txt, 02-random-030.txt, 02-random-031.txt, 02-random-032.txt, 02-random-033.txt, 02-random-034.txt, 02-random-035.txt, 02-random-036.txt, 02-random-037.txt, 02-random-038.txt, 02-random-039.txt, 02-random-040.txt, 02-random-041.txt, 02-random-042.txt, 02-random-043.txt, 02-random-044.txt, 02-random-045.txt, 02-random-046.txt, 02-random-047.txt, 02-random-048.txt, 02-random-049.txt, 02-random-050.txt, 02-random-051.txt, 02-random-052.txt, 02-random-053.txt, 02-random-054.txt, 02-random-055.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 97 ms 44776 KiB
01-handmade-001.txt AC 96 ms 44836 KiB
01-handmade-002.txt AC 96 ms 44764 KiB
01-handmade-003.txt AC 94 ms 44684 KiB
01-handmade-004.txt AC 95 ms 44808 KiB
01-handmade-005.txt AC 92 ms 44756 KiB
01-handmade-006.txt AC 373 ms 62416 KiB
01-handmade-007.txt AC 399 ms 62740 KiB
01-handmade-008.txt AC 384 ms 67604 KiB
01-handmade-009.txt AC 401 ms 64244 KiB
01-handmade-010.txt AC 378 ms 61976 KiB
01-handmade-011.txt AC 401 ms 62860 KiB
01-handmade-012.txt AC 389 ms 63176 KiB
01-handmade-013.txt AC 388 ms 62564 KiB
01-handmade-014.txt AC 380 ms 62080 KiB
01-handmade-015.txt AC 374 ms 60736 KiB
01-handmade-016.txt AC 393 ms 68132 KiB
01-handmade-017.txt AC 415 ms 68656 KiB
01-handmade-018.txt AC 415 ms 64372 KiB
01-handmade-019.txt AC 417 ms 64256 KiB
01-handmade-020.txt AC 418 ms 64484 KiB
01-handmade-021.txt AC 426 ms 64404 KiB
01-handmade-022.txt AC 424 ms 68200 KiB
01-handmade-023.txt AC 420 ms 64520 KiB
01-handmade-024.txt AC 420 ms 65204 KiB
01-handmade-025.txt AC 420 ms 64660 KiB
01-handmade-026.txt AC 429 ms 68460 KiB
01-handmade-027.txt AC 421 ms 63848 KiB
02-random-001.txt AC 97 ms 44716 KiB
02-random-002.txt AC 96 ms 44608 KiB
02-random-003.txt AC 95 ms 44788 KiB
02-random-004.txt AC 102 ms 44660 KiB
02-random-005.txt AC 98 ms 44832 KiB
02-random-006.txt AC 95 ms 44844 KiB
02-random-007.txt AC 95 ms 44864 KiB
02-random-008.txt AC 94 ms 44692 KiB
02-random-009.txt AC 97 ms 44948 KiB
02-random-010.txt AC 94 ms 44656 KiB
02-random-011.txt AC 96 ms 44876 KiB
02-random-012.txt AC 92 ms 44768 KiB
02-random-013.txt AC 93 ms 44692 KiB
02-random-014.txt AC 92 ms 44744 KiB
02-random-015.txt AC 98 ms 44752 KiB
02-random-016.txt AC 149 ms 45632 KiB
02-random-017.txt AC 102 ms 44848 KiB
02-random-018.txt AC 119 ms 45292 KiB
02-random-019.txt AC 131 ms 45284 KiB
02-random-020.txt AC 114 ms 45384 KiB
02-random-021.txt AC 139 ms 45440 KiB
02-random-022.txt AC 97 ms 44948 KiB
02-random-023.txt AC 108 ms 44940 KiB
02-random-024.txt AC 159 ms 45796 KiB
02-random-025.txt AC 102 ms 44924 KiB
02-random-026.txt AC 140 ms 45468 KiB
02-random-027.txt AC 156 ms 45768 KiB
02-random-028.txt AC 153 ms 45672 KiB
02-random-029.txt AC 150 ms 45656 KiB
02-random-030.txt AC 140 ms 45620 KiB
02-random-031.txt AC 266 ms 52860 KiB
02-random-032.txt AC 336 ms 56836 KiB
02-random-033.txt AC 377 ms 62740 KiB
02-random-034.txt AC 378 ms 59660 KiB
02-random-035.txt AC 204 ms 47312 KiB
02-random-036.txt AC 203 ms 47140 KiB
02-random-037.txt AC 258 ms 49148 KiB
02-random-038.txt AC 148 ms 45768 KiB
02-random-039.txt AC 317 ms 58112 KiB
02-random-040.txt AC 361 ms 56592 KiB
02-random-041.txt AC 250 ms 49128 KiB
02-random-042.txt AC 219 ms 47784 KiB
02-random-043.txt AC 386 ms 59536 KiB
02-random-044.txt AC 415 ms 64060 KiB
02-random-045.txt AC 195 ms 47104 KiB
02-random-046.txt AC 361 ms 58488 KiB
02-random-047.txt AC 165 ms 46100 KiB
02-random-048.txt AC 316 ms 55988 KiB
02-random-049.txt AC 172 ms 46192 KiB
02-random-050.txt AC 403 ms 61496 KiB
02-random-051.txt AC 317 ms 57888 KiB
02-random-052.txt AC 326 ms 57992 KiB
02-random-053.txt AC 146 ms 45504 KiB
02-random-054.txt AC 325 ms 58312 KiB
02-random-055.txt AC 390 ms 60368 KiB