-
Notifications
You must be signed in to change notification settings - Fork 0
/
F.kt
47 lines (43 loc) · 1.14 KB
/
F.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
fun main() {
val input = System.`in`.bufferedReader()
val output = StringBuilder()
val s = input.readLine()!!
var n = s.length
val zeroCountAt = IntArray(n) { 0 }
val oneCountAt = IntArray(n) { 0 }
var(zeroCount, oneCount) = listOf(0, 0)
for(i in s.indices) {
if(s[i] == '1') {
oneCount++
} else {
zeroCount++
}
zeroCountAt[i] = zeroCount
oneCountAt[i] = oneCount
}
for(k in 1..n) {
var at = 0
var parts = 0
while(at < n) {
parts++
var(l, r) = listOf(at, n)
while(r > l + 1) {
var m = (l + r) / 2
var count0 = zeroCountAt[m]
var count1 = oneCountAt[m]
if(at - 1 >= 0) {
count0 -= zeroCountAt[at - 1]
count1 -= oneCountAt[at - 1]
}
if(count0 <= k || count1 <= k) {
l = m
} else {
r = m
}
}
at = r
}
output.append("$parts ")
}
print(output)
}