codeforces 5 solution
A
import java.lang.Integer.max
import java.util.*
fun readInt(): Int {
return readLine()!!.toInt()
}
fun readString(): String {
return readLine()!!.trim()
}
fun readIntList(): List<Int> {
return readLine()!!.split(' ').map { it.toInt() }
}
fun main() {
var cost = 0L
var room = mutableSetOf<String>()
while (true) {
var str = readLine()
if (str == null || str.isBlank()) break
str = str.trim()
if (str.startsWith('+')) {
room.add(str.substring(1))
continue
}
if (str.startsWith('-')) {
room.remove(str.substring(1))
continue
}
cost +=
str.split(':')[1]!!.length * room.size
}
println(cost)
}
B
import java.lang.Integer.max
import java.util.*
fun readInt(): Int {
return readLine()!!.toInt()
}
fun readString(): String {
return readLine()!!.trim()
}
fun readIntList(): List<Int> {
return readLine()!!.split(' ').map { it.toInt() }
}
fun main() {
var list = mutableListOf<String>()
while (true) {
var str = readLine()?.trim() ?: break
list.add(str)
}
var max = list.maxOf { it.length }
var toggle = true
println("*".repeat(max + 2))
for (s in list) {
var l = s.length
print('*')
var left = (max - s.length) / 2
var right = max - s.length - left
if (left != right) {
if (!toggle) {
right--
left++
}
toggle = !toggle
}
print(" ".repeat(left))
print(s)
print(" ".repeat(right))
println('*')
}
println("*".repeat(max + 2))
}
C
import java.lang.Integer.max
import java.util.*
fun readInt(): Int {
return readLine()!!.toInt()
}
fun readString(): String {
return readLine()!!.trim()
}
fun readIntList(): List<Int> {
return readLine()!!.split(' ').map { it.toInt() }
}
class Status(var str: String) {
var n: Int
var left = 0
var right = 0
var level = 0
var max = 0
var count = 0
init {
n = str.length
for (ch in str) {
if (ch == '(') level++
else level--
}
right = n - 1
}
fun trim() {
for (i in left..right) {
if (str[i] == ')') {
left++
level++
} else break
}
for (i in right downTo left) {
if (str[i] == '(') {
right--
level--
} else break
}
}
fun solve() {
while (true) {
trim()
if (!(left < right)) break
var c = 0
var l = 0
if (level < 0) {
// start from left
for (i in left..right) {
if (str[i] == '(') c++
else c--
l++
if (c == 0 && (i == right || str[i + 1] == ')')) {
updateMax(l)
left = i + 1
break
}
}
} else {
// start from right
for (i in right downTo left) {
if (str[i] == '(') c++
else c--
l++
if (c == 0 && (i == left || str[i - 1] == '(')) {
updateMax(l)
right = i - 1
break
}
}
}
}
if (count == 0) println("0 1")
else println("$max $count")
}
fun updateMax(newLen: Int) {
if (max < newLen) {
count = 1
max = newLen
return
} else if (max > newLen) {
// pass
} else {
count++
}
}
}
fun main() {
// test()
// return
var str = readLine()!!.trim()
var s = Status(str)
s.solve()
}
fun test() {
// Status(")((())))(()())").solve()
// Status("))(").solve()
// Status("()(())()").solve()
// Status("((((()(((").solve()
// Status("))))()())))").solve()
Status("(()())()(())()()())())()((()(()(())()()())((()(())()(()()()()))()(())()(((()())()(()((())()(())(()))").solve()
}
D
import java.lang.Integer.max
import java.util.*
fun readInt(): Int {
return readLine()!!.toInt()
}
fun readString(): String {
return readLine()!!.trim()
}
fun readIntList(): List<Int> {
return readLine()!!.split(' ').map { it.toInt() }
}
fun readDoubleList(): List<Double> {
return readLine()!!.split(' ').map { it.toDouble() }
}
class Status() {
var a = 0.0
var v = 0.0
var l = 0.0
var d = 0.0
var w = 0.0
fun solve(): Double {
if (v < w) w = v
var cost: Double = 0.0
// to d
// time to d
var t = Math.sqrt(2.0 * d / a)
if (t * a <= w) {
// no limit
t = root2d(a / 2.0, 0.0, -l)
if (vt(a, t) <= v) return t
t = v / a
var r = l - dt(a, t)
t += r / v
return t
} else {
t = w / a
var d1 = dt(a, t)
var t1 = time(w, a, (d - d1) / 2)
var v1 = vt(a, w, t1)
if (v1 <= v) {
t += t1 * 2
} else {
t1 = (v - w) / a
d1 += dt(a, w, t1) * 2
t += t1 * 2
t += (d - d1) / v
}
// suppose not exceed v
var tl = time(w, a, l - d)
if (w + vt(a, tl) < v) {
return t + tl
}
tl = (v - w) / a
var dl = dt(a, w, tl)
return t + tl + (l - d - dl) / v
}
}
fun root2d(a: Double, b: Double, c: Double): Double {
return (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a
}
fun time(
initialVelocity: Double,
accelerator: Double,
distance: Double
): Double {
return root2d(accelerator / 2, initialVelocity, -distance)
}
fun vt(a: Double, t: Double): Double {
return a * t
}
fun vt(a: Double, v: Double, t: Double): Double {
return a * t + v
}
fun dt(a: Double, t: Double): Double {
return a * t * t / 2
}
fun dt(a: Double, v: Double, t: Double): Double {
return a * t * t / 2 + v * t
}
}
fun main() {
var s = Status()
var dl = readDoubleList()
s.a = dl[0]; s.v = dl[1]
dl = readDoubleList()
s.l = dl[0]; s.d = dl[1]; s.w = dl[2]
println("%9.10f".format(s.solve()))
}