diff --git a/storage/zeta/kt/completed/27739.kt b/storage/zeta/kt/completed/27739.kt new file mode 100644 index 0000000..15c505e --- /dev/null +++ b/storage/zeta/kt/completed/27739.kt @@ -0,0 +1,72 @@ +import kotlin.collections.sortedBy + + +fun generate(arr: List, i: Int, j: Int): List> { + val low_ele = arr.getOrElse(i - 1, { Int.MIN_VALUE }) + val high_ele = arr.getOrElse(j, { Int.MAX_VALUE }) + val lists = mutableListOf>().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 { + 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 { + 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)); +} \ No newline at end of file diff --git a/storage/zeta/kt/completed/30484.kt b/storage/zeta/kt/completed/30484.kt new file mode 100644 index 0000000..5eefaf5 --- /dev/null +++ b/storage/zeta/kt/completed/30484.kt @@ -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)) +} \ No newline at end of file diff --git a/storage/zeta/kt/completed/6211.kt b/storage/zeta/kt/completed/6211.kt new file mode 100644 index 0000000..488a615 --- /dev/null +++ b/storage/zeta/kt/completed/6211.kt @@ -0,0 +1,33 @@ +import kotlin.math.max + +fun getTargetCalories(upper: Int, buckets: List): Int { + val deq = mutableListOf>() + + 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 = readln().split(' ').map { i -> i.toInt() } + + println(getTargetCalories(upperCalories, buckets)) +} \ No newline at end of file