Try   HackMD

Competitive Programming in Kotlin


Template

import kotlin.math.* fun readStr() = readln() fun readInt() = readStr().toInt() fun readLong() = readStr().toLong() fun readChars() = readStr().toCharArray() fun readDouble() = readStr().toDouble() fun readInts() = readln().split(" ").map { i -> i.toInt() } fun readLongs() = readln().split(" ").map { i -> i.toLong() } fun readDoubles() = readln().split(" ").map { i -> i.toDouble() } var OUTPUT = StringBuilder() fun out(v: Any) { OUTPUT.append(v) } fun outLine(v: Any = "") = out("$v\n") fun outArray(a: Array<Any>) = out(a.joinToString(" ")) fun outs(vararg v: Any) = outArray(Array(v.size) { i -> v[i] }) fun init() { } fun solve() { } fun main(args: Array<String>) { init() // val t = 1 val t = readInt() for (i in 1..t) { solve() } print(OUTPUT.toString()) }

Notice

Do not use Array<Type>

// use this instead val a = IntArray(N) { 0 } val b = LongArray(N) { 0L } val c = Array(N) { IntArray { 0 } }

Use nullable type in global scope

Sometimes, it is necessary to use global scope in CP, but the size of array is unkown. In this condition, use nullable type to handle the situation.

var adj: Array<ArrayList<Int>>? = null // initialize in main() // pass the reference to variable "adj" instead of value Array(N) { ArrayList<Int>() }.also { adj = it } // method to use the variable for(next in adj!![now]) { ... }