complete 34219.kt 34239.kt

This commit is contained in:
2025-09-10 15:10:06 +09:00
parent 0d7cd3ef04
commit cc53dae710
2 changed files with 155 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
data class Coin(var index: Int, var type: Int, var state: Boolean) {
}
class GoldCoinDetector(val n: Int) {
val coins = mutableListOf<Coin>()
init {
(0 until n).forEach {
coins.add(Coin(it, 0, true))
}
}
var response = -1
var result = -1
fun injectResult() {
response = readLine()!!.toInt()
}
fun processResult() {
// 0 1 2 3
val normal = coins.sumOf { it.type * 9 }
val select = response - normal
coins.forEach {
if (it.type != select) {
it.state = false
}
}
}
fun preQuery(): Boolean {
var cnt = 0
var last = -1
coins.forEach {
it.type = 0
if (it.state) {
cnt++
last = it.index
}
}
if (cnt == 1) {
result = last + 1 // index to real order
return true
}
return false
}
fun query() {
val query = IntArray(n)
coins.filter { it.state }.withIndex().forEach {
it.value.type = it.index % 6
}
coins.forEach {
query[it.index] = it.type
}
println("? ${query.joinToString(" ")}")
System.out.flush()
}
fun claimResult() {
println("! $result")
System.out.flush()
}
}
fun main() {
val n = readLine()!!.toInt()
val detector = GoldCoinDetector(n)
while (!detector.preQuery()) {
detector.query()
detector.injectResult()
detector.processResult()
}
detector.claimResult()
}

View File

@@ -0,0 +1,67 @@
import kotlin.math.max
import kotlin.math.min
import kotlin.random.Random
data class Element(
var consMin: Long, var consMax: Long, var onlyOne: Long, var noChoose: Long
) {
fun max(): Long = max(max(consMax, consMin), max(onlyOne, noChoose))
}
fun maximaAlternatingSum(arr: List<Long>): Long {
if (arr.size == 1) {
return arr.first()
}
val dp = mutableListOf<Element>()
dp.add(Element(arr.last(), arr.last(), arr.last(), Long.MIN_VALUE))
for (x in arr.slice(0 until arr.size - 1).reversed()) {
val last = dp.last()
val e: Element = Element(0, 0, 0, 0)
e.consMax = max(x - last.consMin, x - last.onlyOne)
e.consMin = min(x - last.consMax, x - last.onlyOne)
e.onlyOne = x
e.noChoose = last.max()
//println(e)
dp.add(e)
}
return dp.last().max()
}
fun testAltSum(arr: List<Long>): Long {
var m = Long.MIN_VALUE
val cum1 = mutableListOf<Long>()
val cum2 = mutableListOf<Long>()
cum1.add(0)
cum2.add(0)
var sign = -1
for (item in arr) {
sign *= -1
cum1.add(cum1.last() + sign * item)
cum2.add(cum2.last() - sign * item)
}
for (l in 0 until arr.size) {
for (r in l + 1..arr.size) {
val s = if (l % 2 == 0) {
cum1[r] - cum1[l]
} else {
cum2[r] - cum2[l]
}
m = max(s, m)
}
}
return m
}
fun main() = with(System.`in`.bufferedReader()) {
this.readLine()
val arr = this.readLine().split(" ").map { it.toLong() }
println(maximaAlternatingSum(arr))
}