Skip to content

Instantly share code, notes, and snippets.

@kelvinc1024
Last active May 9, 2021 20:51
Show Gist options
  • Save kelvinc1024/f9b630b53a3d316b3b464dda31e3ef17 to your computer and use it in GitHub Desktop.
Save kelvinc1024/f9b630b53a3d316b3b464dda31e3ef17 to your computer and use it in GitHub Desktop.
[Kotlin] Fast Print Scan Template for Competitive Programming
import java.io.*
import java.util.*
fun main() {
Solution().main()
}
class Solution {
fun main() {
solve()
io.close()
}
fun solve() {
}
fun sort(a: IntArray) {
val l = ArrayList<Int>(a.size)
for (i in a) l.add(i)
Collections.sort(l)
for (i in a.indices) a[i] = l[i]
}
//-----------PrintWriter for faster output---------------------------------
var io = FastIO()
//-----------MyScanner class for faster input----------
class FastIO : PrintWriter {
private var stream: InputStream
private val buf = ByteArray(1 shl 16)
private var curChar = 0
private var numChars = 0
// standard input
@JvmOverloads
constructor(i: InputStream = System.`in`, o: OutputStream? = System.out) : super(o) {
stream = i
}
// file input
constructor(i: String?, o: String?) : super(FileWriter(o)) {
stream = FileInputStream(i)
}
// throws InputMismatchException() if previously detected end of file
private fun nextByte(): Int {
if (numChars == -1) throw InputMismatchException()
if (curChar >= numChars) {
curChar = 0
numChars = try {
stream.read(buf)
} catch (e: IOException) {
throw InputMismatchException()
}
if (numChars == -1) return -1 // end of file
}
return buf[curChar++].toInt()
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
operator fun next(): String {
var c: Int
do {
c = nextByte()
} while (c <= ' '.toInt())
val res = StringBuilder()
do {
res.appendCodePoint(c)
c = nextByte()
} while (c > ' '.toInt())
return res.toString()
}
fun nextLine(): String {
var c: Int
do {
c = nextByte()
} while (c < '\n'.toInt())
val res = StringBuilder()
do {
res.appendCodePoint(c)
c = nextByte()
} while (c > '\n'.toInt())
return res.toString()
}
fun nextInt(): Int { // nextLong() would be implemented similarly
var c: Int
do {
c = nextByte()
} while (c <= ' '.toInt())
var sgn = 1
if (c == '-'.toInt()) {
sgn = -1
c = nextByte()
}
var res = 0
do {
if (c < '0'.toInt() || c > '9'.toInt()) throw InputMismatchException()
res = 10 * res + c - '0'.toInt()
c = nextByte()
} while (c > ' '.toInt())
return res * sgn
}
fun nextDouble(): Double {
return next().toDouble()
}
}
//--------------------------------------------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment