migrate small contest

This commit is contained in:
2026-05-04 14:59:37 +09:00
parent cac746a69c
commit d6777983a1
12 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
import java.io.StreamTokenizer
fun rectanglePermCount(n: Int, table: Array<Array<Int>>): Int {
var cnt = 0
for (r1 in 0 until n) {
for (c1 in 0 until n) {
for (r2 in r1 until n) {
for (c2 in c1 until n) {
val list: MutableList<Int> = mutableListOf()
for (i in r1..r2) {
for (j in c1..c2) {
list.add(table[i][j])
}
}
list.sort()
if (list.withIndex().all { it.index + 1 == it.value }) {
cnt++
}
}
}
}
}
return cnt
}
fun main() = with(StreamTokenizer(System.`in`.bufferedReader())) {
nextToken()
val n = nval.toInt()
val table = Array(n) { Array(n) { 0 } }
for (i in 0 until n) {
for (j in 0 until n) {
nextToken()
table[i][j] = nval.toInt()
}
}
println(rectanglePermCount(n, table))
}

View File

@@ -0,0 +1,29 @@
fun getMaximumEfficientModules(modules: List<Long>): List<Int> {
val sortedModules = modules.withIndex().sortedBy { it.value }
var maxEff = sortedModules[modules.size - 1].value * 3
var curEff = maxEff
var maxBound = modules.size - 1
for (i in modules.size - 2 downTo 0) {
val delta = 2 * sortedModules[i].value - sortedModules[i + 1].value
curEff += delta
if(curEff > maxEff) {
maxEff = curEff
maxBound = i
}
}
return sortedModules.slice(maxBound until sortedModules.size).map {
it.index + 1
}
}
fun main() = with(System.`in`.bufferedReader()) {
this.readLine()
val modules = this.readLine().split(" ").map { it.toLong() }
getMaximumEfficientModules(modules).run {
println(this.size)
println(this.joinToString(" "))
}
}

View File

@@ -0,0 +1,45 @@
fun main() = with(System.`in`.bufferedReader()) {
val n = this.readLine().toInt()
var locations = Array(n * n) { 0 to 0 }
for (i in 0 until n) {
this.readLine().split(" ").map { it.toInt() }.forEachIndexed { j, v ->
locations[v - 1] = i to j
}
}
//
var boxLeftTop = locations[0].first to locations[0].second
var boxRightTop = locations[0].first to locations[0].second
var boxLeftDown = locations[0].first to locations[0].second
var boxRightDown = locations[0].first to locations[0].second
var cnt = 1
for (item in 1 until n * n) {
val row = locations[item].first
val col = locations[item].second
if (boxLeftTop.first <= row && boxLeftTop.second >= col) {
boxLeftTop = row to col
}
if (boxRightTop.first <= row && boxRightTop.second <= col) {
boxRightTop = row to col
}
if (boxLeftDown.first >= row && boxLeftDown.second >= col) {
boxLeftDown = row to col
}
if (boxRightDown.first >= row && boxRightDown.second <= col) {
boxRightDown = row to col
}
if (boxLeftTop.first == boxRightTop.first && boxLeftDown.first == boxRightDown.first) {
if (boxLeftTop.second == boxLeftDown.second && boxRightTop.second == boxRightDown.second) {
if ((boxRightTop.first - boxLeftDown.first + 1) * (boxRightTop.second - boxLeftDown.second + 1) == item + 1) {
cnt++
}
}
}
}
println(cnt)
}

View File

@@ -0,0 +1,16 @@
import java.io.StreamTokenizer
fun main() = with(StreamTokenizer(System.`in`.bufferedReader())) {
nextToken()
val n = nval.toInt()
nextToken()
val m = nval.toInt()
val edges = (1..m).map {
nextToken()
val u = nval.toInt() - 1
nextToken()
val v = nval.toInt() - 1
nextToken()
val w = nval.toLong()
}
}

View File

@@ -0,0 +1,49 @@
import java.util.Stack
fun countNearestFarRelation(n: Int, edges: Array<MutableList<Pair<Int, Int>>>): Long {
val originXor = IntArray(n)
val deq = Stack<Pair<Int, Int>>()
deq.add(0 to 0)
while (deq.isNotEmpty()) {
val (now, before) = deq.pop()
for ((nxt, w) in edges[now]) {
if (nxt == before) {
continue
} else {
originXor[nxt] = originXor[now] xor w
deq.add(nxt to now)
}
}
}
originXor.sort()
var cnt: Long = 0
var before = -1
var cntSliced: Long = 0
for (i in originXor) {
if (i == before) {
cntSliced++
} else {
cnt += (cntSliced) * (cntSliced - 1) / 2
before = i
cntSliced = 1
}
}
cnt += (cntSliced) * (cntSliced - 1) / 2
return cnt
}
fun main() = with(System.`in`.bufferedReader()) {
val n = this.readLine().toInt()
var edges: Array<MutableList<Pair<Int, Int>>> = Array(n + 1) { mutableListOf() }
for (i in 0 until (n - 1)) {
val (u, v, w) = this.readLine().split(" ").map { it.toInt() }
edges[u - 1].add(v - 1 to w)
edges[v - 1].add(u - 1 to w)
}
println(countNearestFarRelation(n, edges))
}

View File

@@ -0,0 +1,20 @@
fun minCostAvoid(intervals: List<Pair<Int, Int>>, laserRanges: List<Pair<Int, Int>>): List<Pair<Int, Int>> {
val (n: Int, q: Int) = Pair(intervals.size, laserRanges.size)
}
fun main() = with(System.`in`.bufferedReader()) {
val (n, q) = this.readLine().split(' ').map { it.toInt() }
val intervals = (1..n).map {
val split = this.readLine().split(' ').map { it.toInt() }
Pair(split[0], split[1])
}
val laserRanges = (1..q).map {
val split = this.readLine().split(' ').map { it.toInt() }
Pair(split[0], split[1])
}
}

View File

@@ -0,0 +1,31 @@
import java.io.StreamTokenizer
fun calcs(maxCycle: Int, calcs: List<Pair<Int, Int>>, query: List<Int>) {
var sCalcs = calcs.sortedBy { it.first }
}
fun main() = with(StreamTokenizer(System.`in`.bufferedReader())) {
nextToken()
val n = nval.toInt()
nextToken()
val maxCycle = nval.toInt()
val calcs = (1..n).map {
nextToken()
val s = nval.toInt()
nextToken()
val e = nval.toInt()
Pair(s, e)
}
val q = nval.toInt()
val times = (1..q).map {
nextToken()
nval.toInt()
}
}

View File

@@ -0,0 +1,85 @@
import java.io.StreamTokenizer
const val K: Long = 1_000_000_007
fun find(parents: IntArray, a: Int): Int {
if (parents[a] == a) {
return a
}
parents[a] = find(parents, parents[a]);
return parents[a]
}
fun union(parents: IntArray, a: Int, b: Int) {
val ra = find(parents, a)
val rb = find(parents, b)
if (ra == rb) {
return
} else if (ra < rb) {
parents[rb] = ra
} else {
parents[ra] = rb
}
}
fun intervalPollutionArea(polluted: List<Long>): Pair<Int, Int> {
val parents = IntArray(polluted.size)
val poses = mutableListOf<Long>()
var now_x = 1L
var before_y = 0L
for ((i, p) in polluted.withIndex()) {
if (p <= before_y) {
now_x++
}
before_y = p
val now_pos: Long = K * now_x + p
poses.add(now_pos)
parents[i] = i
}
for ((i, p) in poses.withIndex()) {
val px = p / K
val py = p % K
val dx = listOf(
K * (px + 1) + py,
p + 1,
)
for (x in dx) {
val findPos = poses.binarySearch(x)
if (findPos > 0) {
union(parents, i, findPos)
}
}
}
return Pair(parents.map { find(parents, it) }.distinct().count(), polluted.size)
}
fun main() = with(StreamTokenizer(System.`in`.bufferedReader())) {
nextToken()
val n = nval.toInt()
val polluted = (1..n).map {
nextToken()
this.nval.toLong()
}
intervalPollutionArea(polluted).run {
println(this.first)
println(this.second)
}
}