-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.kt
42 lines (38 loc) · 1.2 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
enum class InequationType {
LT, // <
GT, // >
KT, // Nothing
}
data class Minmax(val min: Long, val max: Long)
fun solve(previous: Int, choice: Long, seq: List<InequationType>, available: Set<Int>): Minmax {
var min = 9999999999L
var max = 0L
if (seq.isEmpty()) {
return Minmax(choice, choice)
}
for (i in available) {
val canUse = when (seq[0]) {
InequationType.KT -> true
InequationType.LT -> previous < i
InequationType.GT -> previous > i
}
if (!canUse) {
continue
}
val result = solve(i, choice * 10 + i, seq.subList(1, seq.size), available - i)
min = minOf(min, result.min)
max = maxOf(max, result.max)
}
return Minmax(min, max)
}
fun main(args: Array<out String>) {
val k = readLine()!!.toInt()
val a = listOf(InequationType.KT) + readLine()!!.split(' ')
.asSequence()
.filter { it.isNotEmpty() }
.map { if (it == "<") InequationType.LT else InequationType.GT }
.toList()
val (min, max) = solve(0, 0L, a, (0..9).toSet())
println(max.toString().padStart(k + 1, '0'))
println(min.toString().padStart(k + 1, '0'))
}