diff --git "a/src/main/kotlin/hyunsoo/54week/\353\254\270\354\236\220\354\227\264 \352\262\214\354\236\204 2.kt" "b/src/main/kotlin/hyunsoo/54week/\353\254\270\354\236\220\354\227\264 \352\262\214\354\236\204 2.kt" new file mode 100644 index 00000000..c9bb341f --- /dev/null +++ "b/src/main/kotlin/hyunsoo/54week/\353\254\270\354\236\220\354\227\264 \352\262\214\354\236\204 2.kt" @@ -0,0 +1,98 @@ +package hyunsoo.`54week` + +/** + * + * <문제> + * [문자열 게임 2](https://www.acmicpc.net/problem/20437) + * + * - 아이디어 + * + * - 트러블 슈팅 + * + */ +class `전현수_문자열_게임_2` { + + val br = System.`in`.bufferedReader() + + private data class CharWithIndex(val char: Char, val index: Int) + + fun solution() { + + val t = br.readLine().toInt() + + repeat(t) { + + var firstConditionResult = Int.MAX_VALUE + var secondConditionResult = -1 + + val w = br.readLine() + val k = br.readLine().toInt() + + val alphabetCounter = IntArray(26) + val validCharList = mutableSetOf() + + val validCharInfoList = mutableListOf() + + if (k == 1) { + println("1 1") + return@repeat + } + + w.forEachIndexed { index, char -> + val curCharCnt = ++alphabetCounter[char.toSequence()] + if (k <= curCharCnt) + validCharList.add(char) + } + + w.forEachIndexed { index, char -> + if (validCharList.contains(char)) { + validCharInfoList.add(CharWithIndex(char, index)) + } + } + + val sortedValidCharInfoList = validCharInfoList + .sortedWith( + compareBy { it.char } + .thenBy { it.index } + ) + + for (i in sortedValidCharInfoList.indices) { + + val (target, startIndex) = sortedValidCharInfoList[i] + var targetIncludedCnt = 1 + + for (j in i + 1 until sortedValidCharInfoList.size) { + + val (endTarget, endIndex) = sortedValidCharInfoList[j] + if (target != endTarget) { + break + } + targetIncludedCnt++ + if (k != targetIncludedCnt) continue + + val curLength = endIndex - startIndex + 1 + + if (curLength < firstConditionResult) firstConditionResult = curLength + if (secondConditionResult < curLength) secondConditionResult = curLength + + } + } + + if (firstConditionResult == Int.MAX_VALUE || secondConditionResult == -1) { + println(-1) + } else { + println("$firstConditionResult $secondConditionResult") + } + + } + } + + private fun Char.toSequence(): Int { + return this.code - 97 + } + +} + +fun main() { + 전현수_문자열_게임_2().solution() +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/54week/\354\232\260\354\262\264\352\265\255.kt" "b/src/main/kotlin/hyunsoo/54week/\354\232\260\354\262\264\352\265\255.kt" new file mode 100644 index 00000000..84e6d0c7 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/54week/\354\232\260\354\262\264\352\265\255.kt" @@ -0,0 +1,53 @@ +package hyunsoo.`54week` + +/** + * + * <문제> + * [우체국](https://www.acmicpc.net/problem/2141) + * + * - 아이디어 + * + * - 트러블 슈팅 + * + */ +class `전현수_우체국` { + + private data class VillageInfo(val villageNum: Long, val peopleCnt: Long) + + private val villageInfoList = mutableListOf() + + fun solution() { + + var totalPeopleCnt = 0L + var prefixSumOfPeopleCnt = 0L + val n = readln().toInt() + + repeat(n) { + + val (villageNum, peopleCnt) = readln().split(" ").map { it.toLong() } + villageInfoList.add(VillageInfo(villageNum, peopleCnt)) + totalPeopleCnt += peopleCnt + + } + + val mid = if (totalPeopleCnt % 2 == 0L) totalPeopleCnt / 2 else totalPeopleCnt / 2 + 1 + + villageInfoList + .sortedBy { it.villageNum } + .forEach { + + val (vil, curPeopleCnt) = it + + prefixSumOfPeopleCnt += curPeopleCnt + + if (mid <= prefixSumOfPeopleCnt) { + println(vil) + return + } + } + } +} + +fun main() { + 전현수_우체국().solution() +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/54week/\355\206\240\353\247\210\355\206\240.kt" "b/src/main/kotlin/hyunsoo/54week/\355\206\240\353\247\210\355\206\240.kt" new file mode 100644 index 00000000..6037d5d9 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/54week/\355\206\240\353\247\210\355\206\240.kt" @@ -0,0 +1,87 @@ +package hyunsoo.`54week` + +import java.util.LinkedList +import java.util.Queue + +/** + * + * <문제> + * [토마토](https://www.acmicpc.net/problem/7576) + * + * - 아이디어 + * + * - 트러블 슈팅 + * + */ +class `전현수_토마토` { + + private data class Position(val x: Int, val y: Int) + + private val dirs = listOf( + Position(0, -1), + Position(-1, 0), + Position(0, 1), + Position(1, 0), + ) + + private data class QueueData(val pos: Position, val day: Int) + + private val box = mutableListOf>() + private val startPositionList = mutableListOf() + + fun solution() { + + val queue: Queue = LinkedList() + + val (n, m) = readln().split(" ").map { it.toInt() } + + repeat(m) { rowIndex -> + val row = readln().split(" ") + .mapIndexed { columnIndex, it -> + if (it == "1") startPositionList.add(Position(rowIndex, columnIndex)) + it.toInt() + } as MutableList + box.add(row) + } + + startPositionList.forEach { + queue.add(QueueData(it, 1)) + } + + while (queue.isNotEmpty()) { + + val (curPos, curDay) = queue.poll() + + dirs.forEach { dir -> + + val nx = curPos.x + dir.x + val ny = curPos.y + dir.y + + if (nx !in 0 until m || + ny !in 0 until n + ) return@forEach + + if (box[nx][ny] == 0) { + + box[nx][ny] = curDay + 1 + queue.add(QueueData(Position(nx, ny), curDay + 1)) + + } + } + } + + val result = box.flatten() + + if (result.contains(0)) { + println(-1) + } else { + println(result.maxOf { it } - 1) + } + + } + +} + +fun main() { + 전현수_토마토().solution() +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/54week/\355\225\255\354\262\264 \354\235\270\354\213\235.kt" "b/src/main/kotlin/hyunsoo/54week/\355\225\255\354\262\264 \354\235\270\354\213\235.kt" new file mode 100644 index 00000000..98b07ac9 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/54week/\355\225\255\354\262\264 \354\235\270\354\213\235.kt" @@ -0,0 +1,104 @@ +package hyunsoo.`54week` + +import kotlin.system.exitProcess + +/** + * + * <문제> + * [항체 인식](https://www.acmicpc.net/problem/22352) + * + * - 아이디어 + * 영역 별 구분되어있는 모양이 같아야 함. + * 그리고 하나의 영역을 제외하고는 숫자가 같아야 함. + * + * - 트러블 슈팅 + * + */ +class `전현수_항체_인식` { + + private data class Position(val x: Int, val y: Int) + + private val dirs = listOf( + Position(0, -1), + Position(-1, 0), + Position(0, 1), + Position(1, 0), + ) + + private val origin = mutableListOf>() + private val expected = mutableListOf>() + + fun solution() { + + val (n, m) = readln().split(" ").map { it.toInt() } + + repeat(n) { + val row = readln().split(" ").map { it.toInt() } + origin.add(row as MutableList) + } + + repeat(n) { + val row = readln().split(" ").map { it.toInt() } + expected.add(row as MutableList) + } + + for (i in 0 until origin.size) { + for (j in 0 until origin.first().size) { + + val curNumForOrigin = origin[i][j] + val curNumForExpected = expected[i][j] + + if (curNumForOrigin != curNumForExpected) { + + val visited = Array(n) { + BooleanArray(m) + } + + dfs(curNumForOrigin, curNumForExpected, Position(i, j), origin, visited) + + if (origin.flatten() == expected.flatten()) println("YES") + else println("NO") + exitProcess(0) + } + } + } + + println("YES") + + } + + private fun dfs( + targetNum: Int, + expectNumber: Int, + targetPos: Position, + target: MutableList>, + visited: Array, + ) { + + val curNum = target[targetPos.x][targetPos.y] + + if (targetNum != curNum) return + + visited[targetPos.x][targetPos.y] = true + target[targetPos.x][targetPos.y] = expectNumber + + dirs.forEach { dir -> + + val nx = targetPos.x + dir.x + val ny = targetPos.y + dir.y + + if (nx !in 0 until target.size || + ny !in 0 until target.first().size || + visited[nx][ny] + ) return@forEach + + dfs(targetNum, expectNumber, Position(nx, ny), target, visited) + } + + } + +} + +fun main() { + 전현수_항체_인식().solution() +} \ No newline at end of file