From 1b8b416ab78fd43e4d480e7ff3ea4c6a7e12bd8a Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Sun, 10 Mar 2024 20:01:58 +0900 Subject: [PATCH] solve 53week --- ...\352\265\254\355\225\230\352\270\260 5.kt" | 54 ++++++++++++++++ ...70\353\235\274\354\232\264\353\223\234.kt" | 46 ++++++++++++++ .../53week/\354\236\221\354\227\205.kt" | 52 +++++++++++++++ .../\355\202\244 \354\210\234\354\204\234.kt" | 63 +++++++++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 "src/main/kotlin/heejik/53week/\352\265\254\352\260\204 \355\225\251 \352\265\254\355\225\230\352\270\260 5.kt" create mode 100644 "src/main/kotlin/heejik/53week/\354\204\234\352\260\225\352\267\270\353\235\274\354\232\264\353\223\234.kt" create mode 100644 "src/main/kotlin/heejik/53week/\354\236\221\354\227\205.kt" create mode 100644 "src/main/kotlin/heejik/53week/\355\202\244 \354\210\234\354\204\234.kt" diff --git "a/src/main/kotlin/heejik/53week/\352\265\254\352\260\204 \355\225\251 \352\265\254\355\225\230\352\270\260 5.kt" "b/src/main/kotlin/heejik/53week/\352\265\254\352\260\204 \355\225\251 \352\265\254\355\225\230\352\270\260 5.kt" new file mode 100644 index 00000000..e4bb04b0 --- /dev/null +++ "b/src/main/kotlin/heejik/53week/\352\265\254\352\260\204 \355\225\251 \352\265\254\355\225\230\352\270\260 5.kt" @@ -0,0 +1,54 @@ +package heejik.`53week` + +class `구간 합 구하기 5` { + + private var n: Int = 0 + private var m: Int = 0 + private val board = mutableListOf>() + private val poses = mutableListOf>() + fun solve() { + input() + changeBoard() + poses.forEach { + getPrefixSum(it[0] - 1, it[1] - 1, it[2] - 1, it[3] - 1).also { answer -> + println(answer) + } + } + } + + private fun input() { + readln().split(' ').map { it.toInt() }.also { + n = it.first() + m = it.last() + } + repeat(n) { + board.add(readln().split(' ').map { it.toInt() }.toMutableList()) + } + repeat(m) { + poses.add(readln().split(' ').map { it.toInt() }) + } + } + + private fun changeBoard() { + for (x in 0 until n) { + for (y in 0 until n) { + val minusX = if (x == 0) -1 else x - 1 + val minusY = if (y == 0) -1 else y - 1 + board[x][y] += (if (minusY != -1) board[x][minusY] else 0) + + (if (minusX != -1) board[minusX][y] else 0) - + (if (minusX != -1 && minusY != -1) board[minusX][minusY] else 0) + } + } + } + + private fun getPrefixSum(x1: Int, y1: Int, x2: Int, y2: Int): Int { + val leftSum = if (y1 == 0) 0 else board[x2][y1 - 1] + val upSum = if (x1 == 0) 0 else board[x1 - 1][y2] + val diagonalSum = if (x1 == 0 || y1 == 0) 0 else board[x1 - 1][y1 - 1] + return board[x2][y2] - leftSum - upSum + diagonalSum + } +} + +fun main() { + `구간 합 구하기 5`().solve() +} \ No newline at end of file diff --git "a/src/main/kotlin/heejik/53week/\354\204\234\352\260\225\352\267\270\353\235\274\354\232\264\353\223\234.kt" "b/src/main/kotlin/heejik/53week/\354\204\234\352\260\225\352\267\270\353\235\274\354\232\264\353\223\234.kt" new file mode 100644 index 00000000..5292e740 --- /dev/null +++ "b/src/main/kotlin/heejik/53week/\354\204\234\352\260\225\352\267\270\353\235\274\354\232\264\353\223\234.kt" @@ -0,0 +1,46 @@ +package heejik.`53week` + +import kotlin.math.max + +class 서강그라운드 { + + fun solve() { + val (n, m, r) = readln().split(' ').map { it.toInt() } + val itemCounts = readln().split(' ').map { it.toInt() }.toMutableList() + itemCounts.add(0, 0) + val distances = List(size = n + 1) { MutableList(size = n + 1) { 16 } } + var answer = 0 + + repeat(r) { + val (a, b, distance) = readln().split(' ').map { it.toInt() } + distances[a][b] = distance + distances[b][a] = distance + } + + for (k in 1..n) { + for (i in 1..n) { + for (j in 1..n) { + if (distances[i][j] > distances[i][k] + distances[k][j]) { + distances[i][j] = distances[i][k] + distances[k][j] + } + } + } + } + + for (region in 1..n) { + var sum = itemCounts[region] + for (other in 1..n) { + if (region == other) continue + if (distances[region][other] <= m) sum += itemCounts[other] + } + answer = max(answer, sum) + } + + println(answer) + } +} + + +fun main() { + 서강그라운드().solve() +} \ No newline at end of file diff --git "a/src/main/kotlin/heejik/53week/\354\236\221\354\227\205.kt" "b/src/main/kotlin/heejik/53week/\354\236\221\354\227\205.kt" new file mode 100644 index 00000000..b3261f9c --- /dev/null +++ "b/src/main/kotlin/heejik/53week/\354\236\221\354\227\205.kt" @@ -0,0 +1,52 @@ +package heejik.`53week` + +class 작업 { + + private var n = 0 + private var m = 0 + private var findNum = 0 + private var answer = 0 + + // 내부엔 나를 쏘는 애들 + private lateinit var relation: List> + + fun solve() { + input() + bfs() + println(answer) + } + + private fun input() { + readln().split(' ').map { it.toInt() }.also { + n = it[0] + m = it[1] + } + relation = List(size = n + 1) { emptyList().toMutableList() } + repeat(m) { + val (shot, receive) = readln().split(' ').map { it.toInt() } + relation[receive].add(shot) + } + findNum = readln().toInt() + } + + private fun bfs() { + val deque = ArrayDeque() + val visited = BooleanArray(size = n + 1) + deque.add(findNum) + visited[findNum] = true + + while (deque.isNotEmpty()) { + val standard = deque.removeFirst() + relation[standard].forEach { + if (visited[it]) return@forEach + deque.add(it) + visited[it] = true + answer++ + } + } + } +} + +fun main() { + 작업().solve() +} \ No newline at end of file diff --git "a/src/main/kotlin/heejik/53week/\355\202\244 \354\210\234\354\204\234.kt" "b/src/main/kotlin/heejik/53week/\355\202\244 \354\210\234\354\204\234.kt" new file mode 100644 index 00000000..44963c6f --- /dev/null +++ "b/src/main/kotlin/heejik/53week/\355\202\244 \354\210\234\354\204\234.kt" @@ -0,0 +1,63 @@ +package heejik.`53week` + +import java.io.BufferedReader +import java.io.InputStreamReader + +class `키 순서` { + + private var n = 0 + private var m = 0 + private lateinit var relation: List + + val br = BufferedReader(InputStreamReader(System.`in`)) + + fun solve() { + input() + createRelation() + getAnswer().also { answer -> + println(answer) + } + } + + private fun input() { + br.readLine().split(' ').map { it.toInt() }.run { + n = this[0] + m = this[1] + } + relation = List(size = n + 1) { BooleanArray(size = n + 1) } + + repeat(m) { + val (small, large) = br.readLine().split(' ').map { it.toInt() } + relation[small][large] = true + } + } + + private fun createRelation() { + for (k in 1..n) { + for (i in 1..n) { + for (j in 1..n) { + if (relation[i][k] && relation[k][j]) + relation[i][j] = true + } + } + } + } + + private fun getAnswer(): Int { + var answer = 0 + for (i in 1..n) { + var count = 0 + for (j in 1..n) { + if (relation[i][j] || relation[j][i]) count++ + } + if (count == n - 1) answer++ + } + + return answer + } +} + + +fun main() { + `키 순서`().solve() +} \ No newline at end of file