complete 11760.kt 20955.kt
This commit is contained in:
37
storage/zeta/kt/completed/11760.kt
Normal file
37
storage/zeta/kt/completed/11760.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
fun getMastermindFeedback(n: Int, secret: String, guess: String): Pair<Int, Int> {
|
||||
var r: Int = 0
|
||||
var s: Int = 0
|
||||
|
||||
val secretChecked = BooleanArray(n) { false }
|
||||
val guessChecked = BooleanArray(n) { false }
|
||||
|
||||
for (i in 0 until n) {
|
||||
if (secret[i] == guess[i]) {
|
||||
r++
|
||||
secretChecked[i] = true
|
||||
guessChecked[i] = true
|
||||
}
|
||||
}
|
||||
|
||||
for (i in 0 until n) {
|
||||
for (j in 0 until n) {
|
||||
if(secret[i] == guess[j] && !secretChecked[i] && !guessChecked[j]) {
|
||||
secretChecked[i] = true
|
||||
guessChecked[j] = true
|
||||
s++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r to s
|
||||
}
|
||||
|
||||
|
||||
fun main() = with(System.`in`.bufferedReader()) {
|
||||
val (n, a, b) = this.readLine().split(" ").let {
|
||||
Triple(it[0].toInt(), it[1].trim(), it[2].trim())
|
||||
}
|
||||
|
||||
println(getMastermindFeedback(n, a, b).toList().joinToString(" "))
|
||||
}
|
||||
39
storage/zeta/kt/completed/20955.kt
Normal file
39
storage/zeta/kt/completed/20955.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
fun find(parents: IntArray, x: Int): Int {
|
||||
var node = x
|
||||
while (parents[node] != node) {
|
||||
node = parents[node]
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
fun union(parents: IntArray, a: Int, b: Int): Boolean {
|
||||
val ra = find(parents, a)
|
||||
val rb = find(parents, b)
|
||||
if (ra == rb) return false
|
||||
if (ra > rb) parents[rb] = ra else parents[ra] = rb
|
||||
return true
|
||||
}
|
||||
|
||||
fun minimumOperations(n: Int, edges: List<Pair<Int, Int>>): Int {
|
||||
val parents = IntArray(n) { it }
|
||||
var operations = 0
|
||||
edges.forEach { (a, b) ->
|
||||
if (!union(parents, a, b)) {
|
||||
operations++
|
||||
}
|
||||
}
|
||||
|
||||
val rooted_parents = parents.map { find(parents, it) }
|
||||
operations += rooted_parents.distinct().size - 1
|
||||
return operations
|
||||
}
|
||||
|
||||
|
||||
fun main() = with(System.`in`.bufferedReader()) {
|
||||
val (n, m) = this.readLine().split(" ").map { it.toInt() }
|
||||
val synapses: List<Pair<Int, Int>> = (1..m).map {
|
||||
this.readLine().split(" ").map { it.toInt() - 1 }.let { (a, b) -> a to b }
|
||||
}
|
||||
|
||||
println(minimumOperations(n, synapses))
|
||||
}
|
||||
Reference in New Issue
Block a user