complete 6211.kt 27739.kt 30484.kt

This commit is contained in:
2025-07-07 14:33:16 +09:00
parent 86749d2fad
commit 0406a80d8f
3 changed files with 144 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
import kotlin.collections.sortedBy
fun generate(arr: List<Int>, i: Int, j: Int): List<List<Int>> {
val low_ele = arr.getOrElse(i - 1, { Int.MIN_VALUE })
val high_ele = arr.getOrElse(j, { Int.MAX_VALUE })
val lists = mutableListOf<List<Int>>().also {
it.add(
arr.slice(0 until i)
+ arr.slice(i until j)
.sortedBy { item: Int ->
if (item > low_ele) {
Int.MIN_VALUE + item
} else {
item
}
}
+ arr.slice(j until arr.size)
)
it.add(
arr.slice(0 until i)
+ arr.slice(i until j)
.sortedBy { item ->
if (item < high_ele) {
item
} else {
Int.MIN_VALUE + item
}
}
+ arr.slice(j until arr.size)
)
}
return lists
}
fun getIncreasingLength(arr: List<Int>): Int {
var before = -1
var successive = 0
return arr.fold(0) { accum, ele ->
if (ele > before) {
successive++
} else {
successive = 1
}
before = ele
if (accum > successive) {
accum
} else {
successive
}
}
}
fun getMaxRearrangedIncreasingLength(m: Int, arr: List<Int>): Int {
return (0..arr.size - m).maxOf { i ->
generate(arr, i, i + m).maxOf { item ->
getIncreasingLength(item)
}
}
}
fun main() = with(System.`in`.bufferedReader()) {
val (n: Int, m: Int) = readln().split(' ').map { i -> i.toInt() }
val arr = readln().split(' ').map { i -> i.toInt() }.toList()
println(getMaxRearrangedIncreasingLength(m, arr));
}

View File

@@ -0,0 +1,39 @@
const val DIVIDER = 1_000_000_007
data class Counts(
var cntBiggers: Long,
var cntSmallers: Long
)
fun inversionCount(s: String, n: Long): Int {
val alphabetCounts = MutableList(26, { 0 });
val counts = Counts(0, 0)
s.forEach { ch ->
val index = ch.code - 'a'.code
val cntBiggers = alphabetCounts.slice(index + 1 until 26).sum()
val cntSmallers = alphabetCounts.slice(0 until index).sum()
alphabetCounts[index] += 1
counts.cntBiggers += cntBiggers
counts.cntSmallers += cntSmallers
}
val (n_mod: Long, np_mod: Long, nm_mod: Long) = if (n % 2 == 0L) {
Triple((n / 2) % DIVIDER, (n + 1) % DIVIDER, (n - 1) % DIVIDER)
} else {
Triple(n % DIVIDER, ((n + 1) / 2) % DIVIDER, ((n - 1) / 2) % DIVIDER)
}
return ((
(((((counts.cntBiggers % DIVIDER) * np_mod) % DIVIDER) * n_mod) % DIVIDER) + (((((counts.cntSmallers % DIVIDER) * n_mod) % DIVIDER) * nm_mod) % DIVIDER)
) % DIVIDER
).toInt()
}
fun main() = with(System.`in`.bufferedReader()) {
val s = readln().trim()
val n = readln().trim().toLong()
println(inversionCount(s, n))
}

View File

@@ -0,0 +1,33 @@
import kotlin.math.max
fun getTargetCalories(upper: Int, buckets: List<Int>): Int {
val deq = mutableListOf<Pair<Int, Int>>()
deq.add(Pair(0, 0))
var res = -1;
while (deq.isNotEmpty()) {
val (cal, index) = deq.removeLast()
if (index == buckets.size) {
res = max(res, cal)
continue
}
val new0 = cal
val new1 = cal + buckets[index]
deq.add(Pair(new0, index + 1))
if (new1 <= upper) {
deq.add(Pair(new1, index + 1))
}
}
return res
}
fun main() {
val (upperCalories: Int, _) = readln().split(' ').map { i -> i.toInt() }
val buckets: List<Int> = readln().split(' ').map { i -> i.toInt() }
println(getTargetCalories(upperCalories, buckets))
}