-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.kt
144 lines (129 loc) · 3.75 KB
/
solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.*
import kotlin.concurrent.thread
val fastOutStream = FileOutputStream(FileDescriptor.out)
val fastOutBuf = run {
Runtime.getRuntime().addShutdownHook(thread(start = false, name = "fast-io flush() shutdown hook", block = ::flush))
ByteArray(4096)
}
var fastOutBufCount = 0
fun fastOutWrite(s: ByteArray) {
if (s.size >= fastOutBuf.size) {
flush()
fastOutStream.write(s, 0, s.size)
return
}
if (s.size > fastOutBuf.size - fastOutBufCount) {
flush()
}
System.arraycopy(s, 0, fastOutBuf, fastOutBufCount, s.size)
fastOutBufCount += s.size
}
fun flush() {
if (fastOutBufCount > 0) {
fastOutStream.write(fastOutBuf, 0, fastOutBufCount)
fastOutBufCount = 0
}
}
fun print(o: Any?) {
print(o.toString())
}
fun print(s: String) {
fastOutWrite(s.toByteArray())
}
fun println(o: Any?) {
print(o.toString())
print("\n")
}
val fastInStream = FileInputStream(FileDescriptor.`in`)
val fastInBuf = ByteArray(4096)
var fastInBufPos = 0
var fastInBufCount = 0
fun fastInFill() {
fastInBufPos = 0
fastInBufCount = 0
val n = fastInStream.read(fastInBuf, fastInBufPos, fastInBuf.size - fastInBufPos)
if (n > 0)
fastInBufCount = n
}
fun fastInRead(): Byte {
if (fastInBufPos >= fastInBufCount) {
fastInFill()
if (fastInBufPos >= fastInBufCount)
return -1
}
val result = fastInBuf[fastInBufPos]
fastInBufPos++
return result
}
var fastInScanStringBuf: ByteArray? = null
@Suppress("deprecation")
fun scanString(): String {
if (fastInScanStringBuf == null) fastInScanStringBuf = ByteArray(256)
var c: Byte
do {
c = fastInRead()
} while (c == ' '.toByte() || c == '\n'.toByte())
var count = 0
while (c != (-1).toByte() && c != ' '.toByte() && c != '\n'.toByte()) {
if (count == fastInScanStringBuf!!.size) {
val oldBuf = fastInScanStringBuf!!
fastInScanStringBuf = ByteArray(oldBuf.size * 2)
System.arraycopy(oldBuf, 0, fastInScanStringBuf, 0, count)
}
fastInScanStringBuf!![count] = c
count++
c = fastInRead()
}
return java.lang.String(fastInScanStringBuf, 0, 0, count) as String
}
fun scanInt(): Int {
var c: Byte
do {
c = fastInRead()
} while (c == ' '.toByte() || c == '\n'.toByte())
val negative = c == '-'.toByte()
if (negative) c = fastInRead()
var result = 0
while (c != (-1).toByte() && c != ' '.toByte() && c != '\n'.toByte()) {
result = result * 10 + c - '0'.toByte()
c = fastInRead()
}
return if (negative) -result else result
}
fun scanLong(): Long {
var c: Byte
do {
c = fastInRead()
} while (c == ' '.toByte() || c == '\n'.toByte())
val negative = c == '-'.toByte()
if (negative) c = fastInRead()
var result = 0.toLong()
while (c != (-1).toByte() && c != ' '.toByte() && c != '\n'.toByte()) {
result = result * 10 + c - '0'.toByte()
c = fastInRead()
}
return if (negative) -result else result
}
fun scanDouble(): Double {
var c: Byte
do {
c = fastInRead()
} while (c == ' '.toByte() || c == '\n'.toByte())
val negative = c == '-'.toByte()
if (negative) c = fastInRead()
var result = 0.0
while (c != (-1).toByte() && c != ' '.toByte() && c != '\n'.toByte() && c != '.'.toByte()) {
result = result * 10 + c - '0'.toByte()
c = fastInRead()
}
if (c == '.'.toByte()) {
c = fastInRead()
var unit = 1.0
while (c != (-1).toByte() && c != ' '.toByte() && c != '\n'.toByte()) {
unit /= 10
result += (c - '0'.toByte()) * unit
c = fastInRead()
}
}
return if (negative) -result else result
}