diff --git "a/src/3week/byeonghee/\353\213\250\355\222\215\354\236\216 \354\235\264\354\225\274\352\270\260.kt" "b/src/3week/byeonghee/\353\213\250\355\222\215\354\236\216 \354\235\264\354\225\274\352\270\260.kt" new file mode 100644 index 0000000..f30f8db --- /dev/null +++ "b/src/3week/byeonghee/\353\213\250\355\222\215\354\236\216 \354\235\264\354\225\274\352\270\260.kt" @@ -0,0 +1,66 @@ +package `3week`.byeonghee + +import java.io.* + +class `소병희_단풍잎 이야기` { + companion object { + fun getSolution() : Solution { + return Solution() + } + } + + class Solution { + val br = BufferedReader(InputStreamReader(System.`in`)) + + var n = 0 + var m = 0 + + val games = mutableListOf() + val useList = mutableSetOf() + val pickList = mutableSetOf() + var answer = 0 + + fun getBit(n: Int) : Int { + return 1 shl n + } + + fun dfs(bm: Int, pick: Int) { + if (pick == n) { + pickList.add(bm) + return + } + + for(i in useList) { + if (getBit(i) and bm > 0) continue + dfs(bm + getBit(i), pick + 1) + } + } + + + fun solution() { + br.readLine().split(" ").map{ it.toInt() }.let { + n = it[0] + m = it[1] + } + + repeat(m) { + br.readLine().split(" ") + .fold(0) { acc, v -> + useList.add(v.toInt()) + acc + getBit(v.toInt()) + } + .let { games.add(it) } + } + + dfs(0, 0) + + pickList.forEach { bm -> answer = Integer.max(answer, games.count { it and bm == it }) } + + println(answer) + } + } +} + +fun main() { + `소병희_단풍잎 이야기`.getSolution().solution() +} \ No newline at end of file diff --git "a/src/3week/byeonghee/\353\217\204\355\224\274.kt" "b/src/3week/byeonghee/\353\217\204\355\224\274.kt" new file mode 100644 index 0000000..05b45cd --- /dev/null +++ "b/src/3week/byeonghee/\353\217\204\355\224\274.kt" @@ -0,0 +1,74 @@ +package `3week`.byeonghee + +import java.io.* + +class `소병희_도피` { + companion object { + fun getSolution() : Solution { + return Solution() + } + } + + class Solution { + companion object { + const val ONEPROB = 1000000000000000000L + } + + val br = BufferedReader(InputStreamReader(System.`in`)) + + var n = 0 + + var dp = Array(0) { LongArray(0) } + var moves = Array(0) { LongArray(0) } + var ans = 0L + var cityList = mutableListOf() + + fun getDp(day: Int, dst: Int) { + if (dp[day][dst] > -1) return + + var prob = 0L + for(from in 0 until n) { + if(dp[day-1][from] < 0) getDp(day-1, from) + prob += dp[day-1][from] * moves[from][dst] + } + dp[day][dst] = prob + } + + fun solution() { + + val (n_tmp, m) = br.readLine().split(" ").map{ it.toInt() } + n = n_tmp + + dp = Array(9) { LongArray(n){ -1 } } + moves = Array(n) { LongArray(n) } + + repeat(m) { + br.readLine().split(" ").map{ it.toInt() }.let { + moves[it[0]][it[1]] = it[2].toLong() + } + } + + dp[0].fill(0) + moves[0].forEachIndexed { i, v -> dp[0][i] = v } + + for(e in 0 until n) { + getDp(8, e) + if (ans < dp[8][e]) { + ans = dp[8][e] + cityList = mutableListOf(e) + } + else if (ans == dp[8][e]) { + cityList.add(e) + } + } + + println(cityList.joinToString(" ")) + if (ans == ONEPROB) println("1." + "0".repeat(18)) + else println(String.format("0.%18d", ans)) + } + } +} + +fun main() { + `소병희_도피`.getSolution().solution() +} \ No newline at end of file diff --git "a/src/3week/byeonghee/\353\236\234\354\204\240 \354\236\220\353\245\264\352\270\260.kt" "b/src/3week/byeonghee/\353\236\234\354\204\240 \354\236\220\353\245\264\352\270\260.kt" new file mode 100644 index 0000000..654e21d --- /dev/null +++ "b/src/3week/byeonghee/\353\236\234\354\204\240 \354\236\220\353\245\264\352\270\260.kt" @@ -0,0 +1,58 @@ +package `3week`.byeonghee + +import java.io.* + +/** + * 1. Key: 자르는 길이, value: 만들 수 있는 랜선 개수 를 쌍으로 가지는 맵을 아예 만들었었다 + * 2. key가 1부터 2^31 - 1까지 갈 수 있기 때문에 이진탐색을 하면서 key에 대해 value를 계산하는 것이 빠름 + * 3. 2로 바꾸고 나니 key가 int일 필요 없어서 lb, ub, maxLength, mid를 전부 long타입으로 수정 + * 4. 마찬가지로 2로 바꾸고 나니 maxLength가 큰 수여도 괜찮아져서 그냥 제일 긴 랜선 값 할당 + */ + +class `소병희_랜선 자르기` { + companion object { + fun getSolution(): Solution { + return Solution() + } + } + + class Solution { + val br = BufferedReader(InputStreamReader(System.`in`,)) + + var maxLength = 0L + var lans = IntArray(0) +// var searchMap = mapOf() + + var lb = 1L + var ub = 1L + var mid = 1L + + fun solution() { + val (k, n) = br.readLine().split(" ").map { it.toInt() } + lans = IntArray(k) { br.readLine().toInt() } + + maxLength = lans.maxOf { it }.toLong() + //maxLength = Integer.max(lans.minOf { it }, (lans.sumOf { it.toLong() } / n).toInt()) + //searchMap = (1..maxLength).toList().map { len -> len to lans.sumOf { it / len } }.toMap() + + println(bisearch(n)) + } + + fun bisearch(n: Int): Int { + lb = 1 + ub = maxLength + 1 + + while(lb + 1 < ub) { + mid = lb + ((ub - lb) / 2) + if(lans.sumOf { it / mid.toInt() } >= n) lb = mid + else ub = mid + } + + return lb.toInt() + } + } +} + +fun main() { + `소병희_랜선 자르기`.getSolution().solution() +} \ No newline at end of file diff --git "a/src/3week/byeonghee/\353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.kt" "b/src/3week/byeonghee/\353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.kt" new file mode 100644 index 0000000..0f7dfe8 --- /dev/null +++ "b/src/3week/byeonghee/\353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.kt" @@ -0,0 +1,149 @@ +package `3week`.byeonghee + +import java.io.* + +val br = BufferedReader(InputStreamReader(System.`in`)) + +var m = 0 +var s = 0 +var r = 0 +var c = 0 +var d = 0 + +data class Pos(var r: Int, var c: Int) { + operator fun plus(add: Pos): Pos { + return Pos(r + add.r, c + add.c) + } + + fun isIn(): Boolean { + return r in 0 until 4 && c in 0 until 4 + } + + fun flatIdx() : Int { + return r * 4 + c + } +} +data class Fish(var p: Pos, var d: Int) + +val di = arrayOf( + Pos(1, -1), + Pos(1, 0), //하 + Pos(1, 1), + Pos(0, 1), //우 + Pos(-1, 1), + Pos(-1, 0), //상 + Pos(-1, -1), + Pos(0, -1) //좌 +) + +var prevFishCnt = 0 +var curFishCnt = 0 +val prevGrid = IntArray(16) //flatten +val smells = IntArray(16) +val curGrid = IntArray(16) +val fishs = ArrayDeque() +var shark = Pos(0, 0) +val sharkMv = Array(64) { Array(3) { Pos(0, 0) }} +lateinit var newFish: Fish +var canGo = true + + +fun main() { + br.readLine().split(" ").map { it.toInt() }.let { + m = it[0] + s = it[1] + } + + repeat(m) { + br.readLine().split(" ").map { it.toInt() }.let { + fishs.addLast(Fish(Pos(it[0]-1, it[1]-1), 8 - it[2])) + prevGrid[Pos(it[0]-1, it[1]-1).flatIdx()]++ + } + } + + br.readLine().split(" ").map { it.toInt() }.let { + shark.r = it[0] - 1 + shark.c = it[1] - 1 + } + + val dPriority = listOf(5, 7, 1, 3) + var cnt = 0 + + for(f in dPriority) for(s in dPriority) for (t in dPriority) { + sharkMv[cnt++] = arrayOf(di[f], di[f] + di[s], di[f] + di[s] + di[t]) + } + + + var maxCnt = -1 + var tmpCnt: Int + var maxMv = -1 + + for(t in 0 until s) { + // 1, 2번 같이 수행 + prevFishCnt = curFishCnt + curFishCnt = fishs.size + for(i in 0 until fishs.size) { + while (i >= prevFishCnt && smells[fishs[i].p.flatIdx()] == 2) { + fishs.removeAt(i) + curFishCnt-- + if (i == fishs.size) break + } + newFish = fishs[i].copy() + canGo = true + while((newFish.p + di[newFish.d]).isIn().not() + || smells[(newFish.p + di[newFish.d]).flatIdx()] > 0 + || shark == (newFish.p + di[newFish.d])) { + newFish.d = (newFish.d + 1) % 8 + if (newFish.d == fishs[i].d) { + canGo = false + break + } + } + if(canGo) newFish.p += di[newFish.d] + fishs.add(newFish) + curGrid[newFish.p.flatIdx()]++ + } + + //3-1. 상어 이동순서 구하기 + maxCnt = -1 + maxMv = -1 + for(mv in sharkMv.indices) { + if (sharkMv[mv].any { (shark + it).isIn().not() }) continue + + sharkMv[mv].let { + tmpCnt = curGrid[(it[0] + shark).flatIdx()] + tmpCnt += curGrid[(it[1] + shark).flatIdx()] + if (it[2] != it[0]) { + tmpCnt += curGrid[(it[2] + shark).flatIdx()] + } + } + + if (tmpCnt > maxCnt) { + maxCnt = tmpCnt + maxMv = mv + } + } + + //3-2. 상어 잡아먹기 + 냄새 흐려지기 + for(i in 0 until 16) { + if (smells[i] > 0) smells[i]-- + prevGrid[i] += curGrid[i] + } + sharkMv[maxMv].map{ (shark + it).flatIdx() }.forEach { + if (prevGrid[it] > 0) { + smells[it] += 2 + prevGrid[it] -= curGrid[it] + } + } + shark += sharkMv[maxMv][2] + curGrid.fill(0) + } + +// tmpCnt = 0 +// for(i in 0 until 16) { +// if (smells[i] == 2) tmpCnt += prevGrid[i] +// } + +// println(fishs.size - tmpCnt) + println(prevGrid.sumOf { it }) +} \ No newline at end of file diff --git "a/src/3week/byeonghee/\355\201\254\353\241\234\354\212\244\354\233\214\353\223\234.kt" "b/src/3week/byeonghee/\355\201\254\353\241\234\354\212\244\354\233\214\353\223\234.kt" new file mode 100644 index 0000000..742b35a --- /dev/null +++ "b/src/3week/byeonghee/\355\201\254\353\241\234\354\212\244\354\233\214\353\223\234.kt" @@ -0,0 +1,47 @@ +package `3week`.byeonghee + +/** 우선순위큐, split 함수 */ + +import java.io.* +import java.util.* + +class `소병희_크로스워드` { + companion object { + fun getSolution() : Solution { + return Solution() + } + } + + class Solution { + val br = BufferedReader(InputStreamReader(System.`in`)) + + var words = PriorityQueue { a, b -> when { + a > b -> 1 + a < b -> -1 + else -> 0 + }} + + fun solution() { + val (row, col) = br.readLine().split(" ").map{ it.toInt()} + val puzzles = Array(row) { br.readLine() } + + (0 until row).forEach { r -> + puzzles[r].split("#").filter{ it.length > 1 }.let { + words.addAll(it) + } + } + + (0 until col).forEach { c -> + puzzles.map{ it[c] }.joinToString("").split("#").filter{ it.length > 1 }.let { + words.addAll(it) + } + } + + println(words.peek()) + } + } +} + +fun main() { + `소병희_크로스워드`.getSolution().solution() +} \ No newline at end of file diff --git "a/src/3week/byeonghee/\355\202\271.kt" "b/src/3week/byeonghee/\355\202\271.kt" new file mode 100644 index 0000000..b43e002 --- /dev/null +++ "b/src/3week/byeonghee/\355\202\271.kt" @@ -0,0 +1,86 @@ +package `3week`.byeonghee + +import java.io.* + +class `소병희_킹` { + companion object { + fun getSolution() : Solution { + return Solution() + } + } + + + class Solution { + companion object { + fun Char.toIdx() : Int { + return if (this.isDigit()) (digitToInt() - 1) else code - 'A'.code + } + + fun Int.isInside() : Boolean { + return this in 0 until 8 + } + + + fun Int.toBoard() : Char { + return (this + 'A'.code).toChar() + } + } + + val br = BufferedReader(InputStreamReader(System.`in`)) + + data class Pos(val r: Int, val c: Int) { + operator fun plus(pos: Pos): Pos { + val newR = r + pos.r + val newC = c + pos.c + return Pos(newR, newC) + } + + fun isInside() = r.isInside() && c.isInside() + + override fun toString() : String { + return c.toBoard() + (r + 1).toString() + } + } + + fun solution() { + val kingMoving = mapOf( + Pair("R", Pos(0, 1)), + Pair("L", Pos(0, -1)), + Pair("B", Pos(-1, 0)), + Pair("T", Pos(1, 0)), + Pair("RT", Pos(1, 1)), + Pair("LT", Pos(1, -1)), + Pair("RB", Pos(-1, 1)), + Pair("LB", Pos(-1, -1)) + ) + + val (king, stone, n) = br.readLine().split(" ") + var kingPos = Pos(king[1].toIdx(), king[0].toIdx()) + var stonePos = Pos(stone[1].toIdx(), stone[0].toIdx()) + + val moves = mutableListOf() + repeat(n.toInt()) { + moves.add( kingMoving.getOrDefault(br.readLine(), Pos(0, 0))) + } + + for(move in moves) { + if (kingPos + move == stonePos) { + if ((stonePos + move).isInside()) { + kingPos = stonePos + stonePos += move + } + } + else if ((kingPos + move).isInside()) { + kingPos += move + } + } + + println("$kingPos") + println("$stonePos") + } + } +} + +fun main() { + `소병희_킹`.getSolution().solution() +} \ No newline at end of file diff --git "a/src/4week/byeonghee/\352\267\274\354\206\220\354\213\244.kt" "b/src/4week/byeonghee/\352\267\274\354\206\220\354\213\244.kt" new file mode 100644 index 0000000..3d14645 --- /dev/null +++ "b/src/4week/byeonghee/\352\267\274\354\206\220\354\213\244.kt" @@ -0,0 +1,48 @@ +package `4week`.byeonghee + +import java.io.* + +class `소병희_근손실` { + val br = BufferedReader(InputStreamReader(System.`in`)) + + var n = 0 + var k = 0 + var workouts = IntArray(0) + + val visited = BooleanArray(8) { false } + var curPerm = 0 + var answer = 0 + + fun solution() { + br.readLine().split(" ").map { it.toInt() }.run { + n = first() + k = last() + } + workouts = br.readLine().split(" ").map { it.toInt() - k }.toIntArray() + + makePerm(0) + println(answer) + } + + fun makePerm(len: Int) { + if (len == n) { + answer++ + return + } + + for(i in 0 until n) { + if (visited[i]) continue + if (curPerm + workouts[i] < 0) continue + + visited[i] = true + curPerm += workouts[i] + makePerm(len + 1) + curPerm -= workouts[i] + visited[i] = false + } + } +} + +fun main() { + `소병희_근손실`().solution() +} \ No newline at end of file diff --git "a/src/4week/byeonghee/\354\247\200\353\246\204\352\270\270.kt" "b/src/4week/byeonghee/\354\247\200\353\246\204\352\270\270.kt" new file mode 100644 index 0000000..9acb997 --- /dev/null +++ "b/src/4week/byeonghee/\354\247\200\353\246\204\352\270\270.kt" @@ -0,0 +1,56 @@ +package `4week`.byeonghee + +import java.io.* + +val br = BufferedReader(InputStreamReader(System.`in`)) + +var answer = Int.MAX_VALUE +var dist = 0 +var D = 0 +var N = 0 + +data class Edge(val s: Int, val e: Int, val d: Int) +val edgeMap = mutableMapOf>() + +fun main() { + br.readLine().split(" ").map{ it.toInt() }.let { + N = it[0] + D = it[1] + } + + repeat(N) { + val (s, e, d) = br.readLine().split(" ").map{ it.toInt() } + if ((e <= D) && (d < (e - s))) { + if (edgeMap.contains(s)) { + edgeMap[s]!!.add(Edge(s, e, d)) + } + else { + edgeMap[s] = mutableListOf(Edge(s, e, d)) + } + } + } + + dfs(0) + println(answer) +} + +fun dfs(node: Int) { + println(node) + + var nxt = node + var mv = 0 + while(edgeMap.containsKey(nxt).not() && nxt < D) { + nxt++ + mv++ + } + if (nxt == D) { + answer = Integer.min(answer, dist + mv) + return + } + println(nxt) + for(i in edgeMap[nxt]!!) { + dist += i.d + mv + dfs(i.e) + dist -= i.d - mv + } +} \ No newline at end of file diff --git "a/src/4week/byeonghee/\355\212\270\353\246\254\354\235\230 \353\266\200\353\252\250 \354\260\276\352\270\260.kt" "b/src/4week/byeonghee/\355\212\270\353\246\254\354\235\230 \353\266\200\353\252\250 \354\260\276\352\270\260.kt" new file mode 100644 index 0000000..5c2eeb0 --- /dev/null +++ "b/src/4week/byeonghee/\355\212\270\353\246\254\354\235\230 \353\266\200\353\252\250 \354\260\276\352\270\260.kt" @@ -0,0 +1,42 @@ +package `4week`.byeonghee + +import java.io.* + +class `트리의 부모 찾기` { + val br = BufferedReader(InputStreamReader(System.`in`)) + + lateinit var parents : IntArray + val edges = mutableMapOf>() + val q = ArrayDeque>() + + fun solution() { + val n = br.readLine().toInt() + parents = IntArray(n + 1) + + repeat(n-1) { i -> + br.readLine().split(" ").map{ it.toInt() }.run { + edges.getOrPut(first()) { mutableListOf() } + edges[first()]!!.add(last()) + edges.getOrPut(last()) { mutableListOf() } + edges[last()]!!.add(first()) + if (first() == 1) q.add(Pair(first(), last())) + else if (last() == 1) q.add(Pair(last(), first())) + } + } + + lateinit var cur: Pair + while(q.isNotEmpty()) { + cur = q.removeFirst() + parents[cur.second] = cur.first + for(i in edges[cur.second]!!) { + if (parents[i] == 0) q.add(Pair(cur.second, i)) + } + } + + println(parents.drop(2).joinToString("\n")) + } +} + +fun main() { + `트리의 부모 찾기`().solution() +} \ No newline at end of file diff --git "a/src/4week/byeonghee/\355\232\214\354\235\230\354\213\244 \353\260\260\354\240\225.kt" "b/src/4week/byeonghee/\355\232\214\354\235\230\354\213\244 \353\260\260\354\240\225.kt" new file mode 100644 index 0000000..a9ff47d --- /dev/null +++ "b/src/4week/byeonghee/\355\232\214\354\235\230\354\213\244 \353\260\260\354\240\225.kt" @@ -0,0 +1,39 @@ +package `4week`.byeonghee + +import java.io.* +import java.util.PriorityQueue + +class `소병희_회의실 배정` { + + data class Meeting(val s: Int, val e: Int) + + val br = BufferedReader(InputStreamReader(System.`in`)) + val meetings = PriorityQueue(Comparator { a, b -> if (a.e == b.e) a.s - b.s else a.e - b.e }) + + fun solution() { + val n = br.readLine().toInt() + repeat(n) { + br.readLine().split(" ").map{ it.toInt() }.run{ + meetings.add(Meeting(first(), last())) + } + } + + var answer = 0 + var curLastT = 0 + lateinit var nxt : Meeting + + while(meetings.isNotEmpty()) { + nxt = meetings.poll() + if (curLastT <= nxt.s) { + curLastT = nxt.e + answer++ + } + } + + println(answer) + } +} + +fun main() { + `소병희_회의실 배정`().solution() +} \ No newline at end of file