Submission #24757750


Source Code Expand

@file:Suppress("DuplicatedCode")

import java.io.IOException
import java.io.InputStream
import java.io.PrintWriter
import java.util.NoSuchElementException

fun main(args: Array<String>) {
    PrintWriter(System.out).use { out ->
        P006.solve(P006.FastScanner(), out)
        out.flush()
    }
}

private fun P006.solve(sc: P006.FastScanner, out: PrintWriter) {
    val N = sc.nextInt()
    val K = sc.nextInt()

    val cl = Array(26) { ArrayList<Int>() }

    repeat(N) { i ->
        val c = sc.readByteAsInt() - 'a'.toInt()
        cl[c].add(i)
    }

    var lastCharIndex = -1
    for(stringIndex in 0 until K) {
        for (clCursor in 0 until 26) {
            var list = cl[clCursor]

            var hi = list.size
            var lo = -1

            while (hi - lo > 1) {
                val mid = (hi + lo) / 2
                val e = list[mid]
                if (lastCharIndex < e) hi = mid
                else lo = mid
            }

            if (hi == list.size) continue

            val e = list[hi]

            if (e > N - K + stringIndex) continue

            out.print((clCursor + 'a'.toInt()).toChar())
            lastCharIndex = e
            break
        }
    }

    out.println()
}

@Suppress("MemberVisibilityCanBePrivate")
private object P006 {
    private const val zeroCode = '0'.toInt()
    private const val nineCode = '9'.toInt()

    class FastScanner {
        private val `in`: InputStream = System.`in`
        private val buffer = ByteArray(2048)
        private var ptr = 0
        private var buflen = 0

        private fun hasNextByte(): Boolean {
            if (ptr < buflen) {
                return true
            } else {
                ptr = 0
                try {
                    buflen = `in`.read(buffer)
                } catch (e: IOException) {
                    e.printStackTrace()
                }
                if (buflen <= 0) {
                    return false
                }
            }
            return true
        }

        fun readByte(): Byte = if (hasNextByte()) buffer[ptr++] else -1

        fun readByteAsInt(): Int = if (hasNextByte()) buffer[ptr++].toInt() else -1

        operator fun hasNext(): Boolean {
            while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++
            return hasNextByte()
        }

        operator fun next(): String {
            if (!hasNext()) throw NoSuchElementException()

            return buildString {
                var b = readByteAsInt()

                while (isPrintableChar(b)) {
                    appendCodePoint(b)
                    b = readByteAsInt()
                }
            }
        }

        fun toCharIterator(): CharIterator = object : CharIterator() {
            private var nextByte: Byte = readByte()

            override fun hasNext(): Boolean = isPrintableChar(nextByte)

            override fun nextChar(): Char {
                val res = nextByte
                nextByte = readByte()
                return res.toChar()
            }

        }

        fun nextLong(): Long {
            if (!hasNext()) throw NoSuchElementException()
            var n: Long = 0
            var minus = false
            var b = readByteAsInt()

            if (b == '-'.toInt()) {
                minus = true
                b = readByteAsInt()
            }

            if (b !in zeroCode..nineCode) {
                throw NumberFormatException()
            }

            while (true) {
                if (b in zeroCode..nineCode) {
                    n *= 10
                    n += b - zeroCode.toLong()
                } else if (b == -1 || !isPrintableChar(b)) {
                    return if (minus) -n else n
                } else {
                    throw NumberFormatException()
                }

                b = readByteAsInt()
            }
        }

        fun nextInt(): Int {
            val nl = nextLong()
            if (nl < Int.MIN_VALUE || nl > Int.MAX_VALUE) throw NumberFormatException()
            return nl.toInt()
        }

        fun nextDouble(): Double {
            return next().toDouble()
        }

        fun readIntArray(n: Int): IntArray {
            val arr = IntArray(n)

            for (i in 0 until n) {
                arr[i] = nextInt()
            }

            return arr
        }

        fun readBigInt(capacity: Int): IntArray {
            if (!hasNext()) throw NoSuchElementException()

            val arr = IntArray(capacity)
            var len = 0
            arr[0] = readByteAsInt() - zeroCode
            while (arr[len] in 0..9) {
                len++
                arr[len] = readByteAsInt() - zeroCode
            }

            return arr.sliceArray(0 until len)
        }

        companion object {
            private inline fun isPrintableChar(c: Int): Boolean {
                return c in 33..126
            }

            private inline fun isPrintableChar(c: Byte): Boolean {
                return c in 33..126
            }
        }
    }
}

Submission Info

Submission Time
Task 006 - Smallest Subsequence(★5)
User Bookends
Language Kotlin (1.3.71)
Score 5
Code Size 5220 Byte
Status AC
Exec Time 261 ms
Memory 38924 KiB

Compile Error

warning: ATTENTION!
This build uses unsafe internal compiler arguments:

-XXLanguage:+InlineClasses

This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!

Main.kt:183:21: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
            private inline fun isPrintableChar(c: Int): Boolean {
                    ^
Main.kt:187:21: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
            private inline fun isPrintableChar(c: Byte): Boolean {
                    ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 5 / 5
Status
AC × 2
AC × 29
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 10_random_small_00.txt, 10_random_small_01.txt, 11_random_medium_00.txt, 11_random_medium_01.txt, 12_random_large_00.txt, 12_random_large_01.txt, 13_random_max_00.txt, 13_random_max_01.txt, 13_random_max_02.txt, 20_unique_small_00.txt, 20_unique_small_01.txt, 21_unique_medium_00.txt, 21_unique_medium_01.txt, 22_unique_large_00.txt, 22_unique_large_01.txt, 23_unique_max_00.txt, 23_unique_max_01.txt, 23_unique_max_02.txt, 30_equal_small_00.txt, 30_equal_small_01.txt, 31_equal_medium_00.txt, 31_equal_medium_01.txt, 32_equal_large_00.txt, 32_equal_large_01.txt, 33_equal_max_00.txt, 33_equal_max_01.txt, 33_equal_max_02.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 90 ms 33808 KiB
00_sample_01.txt AC 80 ms 33972 KiB
10_random_small_00.txt AC 82 ms 34168 KiB
10_random_small_01.txt AC 80 ms 33792 KiB
11_random_medium_00.txt AC 118 ms 35232 KiB
11_random_medium_01.txt AC 121 ms 35512 KiB
12_random_large_00.txt AC 221 ms 37896 KiB
12_random_large_01.txt AC 171 ms 37412 KiB
13_random_max_00.txt AC 238 ms 37788 KiB
13_random_max_01.txt AC 229 ms 38668 KiB
13_random_max_02.txt AC 112 ms 37732 KiB
20_unique_small_00.txt AC 85 ms 33816 KiB
20_unique_small_01.txt AC 77 ms 33768 KiB
21_unique_medium_00.txt AC 118 ms 36032 KiB
21_unique_medium_01.txt AC 107 ms 35216 KiB
22_unique_large_00.txt AC 163 ms 36652 KiB
22_unique_large_01.txt AC 226 ms 37860 KiB
23_unique_max_00.txt AC 261 ms 38704 KiB
23_unique_max_01.txt AC 212 ms 38000 KiB
23_unique_max_02.txt AC 220 ms 38924 KiB
30_equal_small_00.txt AC 79 ms 33840 KiB
30_equal_small_01.txt AC 95 ms 34636 KiB
31_equal_medium_00.txt AC 152 ms 36256 KiB
31_equal_medium_01.txt AC 118 ms 35348 KiB
32_equal_large_00.txt AC 170 ms 37504 KiB
32_equal_large_01.txt AC 173 ms 37580 KiB
33_equal_max_00.txt AC 197 ms 37956 KiB
33_equal_max_01.txt AC 190 ms 37708 KiB
33_equal_max_02.txt AC 203 ms 37748 KiB