complete 34219.kt 34239.kt
This commit is contained in:
88
storage/zeta/kt/completed/34219.kt
Normal file
88
storage/zeta/kt/completed/34219.kt
Normal 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()
|
||||
}
|
||||
67
storage/zeta/kt/completed/34239.kt
Normal file
67
storage/zeta/kt/completed/34239.kt
Normal 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))
|
||||
}
|
||||
Reference in New Issue
Block a user