Submission #54833583


Source Code Expand

import MutableIntMap.Companion.mutableIntMapOf
import java.io.*
import java.util.*
import kotlin.collections.*
import kotlin.math.*

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()
    }
}

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

@JvmInline
value class ModInt private constructor(val value: Int) {
    operator fun plus(other: ModInt) = plus(other.value)
    operator fun plus(other: Int) = from(value + other)
    operator fun plus(other: Long) = from(value + other)
    operator fun minus(other: ModInt) = minus(other.value)
    operator fun minus(other: Int) = from(value - other)
    operator fun minus(other: Long) = from(value - other)
    operator fun times(other: ModInt) = times(other.value)
    operator fun times(other: Int) = times(other.toLong())
    operator fun times(other: Long) = from(value * other)
    operator fun div(other: ModInt) = times(other.inv())
    operator fun div(other: Int) = div(from(other))
    operator fun div(other: Long) = div(from(other))

    fun pow(exponent: Int): ModInt {
        var ans = One
        var a = this
        var b = exponent
        while (b > 0) {
            if (b % 2 == 1) ans *= a
            a *= a
            b /= 2
        }
        return ans
    }

    fun inv(): ModInt = pow(MOD - 2)

    override fun toString() = value.toString()

    companion object {
        fun combination(n: Int, k: Int): ModInt {
            check(k in 0..n)
            return (1..k).fold(One) { acc, i -> acc * (n - i + 1) / i }
        }

        fun from(value: Int) = ModInt(value.mod())
        fun from(value: Long) = ModInt(value.mod())
        fun Int.mod() = mod(MOD)
        fun Long.mod() = mod(MOD)
        val Zero = from(0)
        val One = from(1)

        const val MOD = 998244353
    }
}

fun Array<ModInt>.sum(): ModInt {
    var sum = ModInt.Zero
    for (element in this) sum += element
    return sum
}


fun solve() {
    val n = readInt()
    val h = readLongs(n)
    val s = ArrayDeque<Pair<Long, Int>>()
    val ans = LongArray(n)
    for (i in 0 until n) {
        while (s.isNotEmpty() && s.last().first <= h[i]) s.removeLast()
        ans[i] = if (s.isEmpty()) {
            h[i] * (i + 1)
        } else {
            val mi = s.last().second
            ans[mi] + h[i] * (i - mi)
        }
        s += h[i] to i
    }
    println(ans.map { it + 1 }.joinToString(" "))
}

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

Submission Info

Submission Time
Task E - Water Tank
User wangchaohui
Language Kotlin (Kotlin/JVM 1.8.20)
Score 500
Code Size 4403 Byte
Status AC
Exec Time 492 ms
Memory 86560 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 41
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 67 ms 40372 KiB
00_sample_01.txt AC 64 ms 40452 KiB
00_sample_02.txt AC 64 ms 40304 KiB
01_random_03.txt AC 448 ms 82004 KiB
01_random_04.txt AC 413 ms 84240 KiB
01_random_05.txt AC 436 ms 82044 KiB
01_random_06.txt AC 452 ms 82072 KiB
01_random_07.txt AC 472 ms 83892 KiB
01_random_08.txt AC 431 ms 82440 KiB
01_random_09.txt AC 453 ms 81960 KiB
01_random_10.txt AC 338 ms 68544 KiB
01_random_11.txt AC 311 ms 63516 KiB
01_random_12.txt AC 432 ms 81932 KiB
01_random_13.txt AC 280 ms 61352 KiB
01_random_14.txt AC 277 ms 61088 KiB
01_random_15.txt AC 418 ms 81432 KiB
01_random_16.txt AC 415 ms 80696 KiB
01_random_17.txt AC 412 ms 81432 KiB
01_random_18.txt AC 390 ms 74360 KiB
01_random_19.txt AC 428 ms 80852 KiB
01_random_20.txt AC 400 ms 80664 KiB
01_random_21.txt AC 428 ms 76008 KiB
01_random_22.txt AC 406 ms 82456 KiB
01_random_23.txt AC 407 ms 80480 KiB
01_random_24.txt AC 431 ms 81396 KiB
01_random_25.txt AC 423 ms 80372 KiB
01_random_26.txt AC 439 ms 86560 KiB
01_random_27.txt AC 416 ms 85048 KiB
01_random_28.txt AC 438 ms 84904 KiB
01_random_29.txt AC 443 ms 84840 KiB
01_random_30.txt AC 452 ms 84944 KiB
01_random_31.txt AC 451 ms 84956 KiB
01_random_32.txt AC 492 ms 84824 KiB
01_random_33.txt AC 480 ms 85468 KiB
01_random_34.txt AC 427 ms 86156 KiB
01_random_35.txt AC 450 ms 84996 KiB
01_random_36.txt AC 436 ms 84656 KiB
01_random_37.txt AC 68 ms 40540 KiB
01_random_38.txt AC 66 ms 40500 KiB
01_random_39.txt AC 332 ms 68484 KiB
01_random_40.txt AC 434 ms 81320 KiB