From 80a144873c5f075ea95476d13257e1b0628a3da1 Mon Sep 17 00:00:00 2001 From: bngsh Date: Sun, 5 Mar 2023 14:14:53 +0900 Subject: [PATCH 1/5] =?UTF-8?q?solve:=20=ED=91=9C=ED=98=84=20=EA=B0=80?= =?UTF-8?q?=EB=8A=A5=ED=95=9C=20=EC=9D=B4=EC=A7=84=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\354\247\204\355\212\270\353\246\254.kt" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "src/main/kotlin/byeonghee/week26/\355\221\234\355\230\204 \352\260\200\353\212\245\355\225\234 \354\235\264\354\247\204\355\212\270\353\246\254.kt" diff --git "a/src/main/kotlin/byeonghee/week26/\355\221\234\355\230\204 \352\260\200\353\212\245\355\225\234 \354\235\264\354\247\204\355\212\270\353\246\254.kt" "b/src/main/kotlin/byeonghee/week26/\355\221\234\355\230\204 \352\260\200\353\212\245\355\225\234 \354\235\264\354\247\204\355\212\270\353\246\254.kt" new file mode 100644 index 00000000..583e42f2 --- /dev/null +++ "b/src/main/kotlin/byeonghee/week26/\355\221\234\355\230\204 \352\260\200\353\212\245\355\225\234 \354\235\264\354\247\204\355\212\270\353\246\254.kt" @@ -0,0 +1,43 @@ +package byeonghee.week26 + +class `소병희_표현 가능한 이진트리` { + + val sb = StringBuilder() + + fun solution(numbers: LongArray): IntArray { + var answer: IntArray = IntArray(numbers.size) + + var trimmedSize = 0 + var perfectSize = 0 + + for(i in numbers.indices) { + sb.clear() + sb.append(java.lang.Long.toBinaryString(numbers[i])) + + trimmedSize = sb.length + perfectSize = getTreeSize(trimmedSize) + sb.insert(0, "0".repeat(perfectSize - trimmedSize)) + answer[i] = checkSubTree(0, perfectSize - 1, 1) + } + + return answer + } + + fun getTreeSize(len: Int) : Int { + var ret = 1 + while(ret <= len) { + ret *= 2 + } + return ret - 1 + } + + fun checkSubTree(s: Int, e: Int, parent: Int) : Int { + val mid = (s + e) / 2 + val root = sb[mid] - '0' + if (root == 1 && parent == 0) return 0 + if (s == e) return 1 + + if (checkSubTree(s, mid - 1, root) == 0) return 0 + return checkSubTree(mid + 1, e, root) + } +} \ No newline at end of file From bfa140923333e0590d04219ca869ee04df2af968 Mon Sep 17 00:00:00 2001 From: bngsh Date: Tue, 7 Mar 2023 20:00:54 +0900 Subject: [PATCH 2/5] =?UTF-8?q?solve:=20=EC=A0=88=EB=8C=93=EA=B0=92=20?= =?UTF-8?q?=ED=9E=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\353\214\223\352\260\222 \355\236\231.kt" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 "src/main/kotlin/byeonghee/week26/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" diff --git "a/src/main/kotlin/byeonghee/week26/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" "b/src/main/kotlin/byeonghee/week26/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" new file mode 100644 index 00000000..ad2705e3 --- /dev/null +++ "b/src/main/kotlin/byeonghee/week26/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" @@ -0,0 +1,19 @@ +package byeonghee.week26 + +import java.io.* +import java.util.PriorityQueue +import kotlin.math.abs + +fun main(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) { + val n = readLine().toInt() + val pq = PriorityQueue { a, b -> if (a + b == 0) a - b else abs(a) - abs(b) } + + var x : Int + repeat(n) { + x = readLine().toInt() + when(x) { + 0 -> println(pq.poll() ?: "0") + else -> pq.add(x) + } + } +} \ No newline at end of file From 17838a788479402219c89df9752bdb19699f7c3a Mon Sep 17 00:00:00 2001 From: bngsh Date: Wed, 8 Mar 2023 20:14:05 +0900 Subject: [PATCH 3/5] =?UTF-8?q?solve:=20=EC=B9=98=EC=A6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../week26/\354\271\230\354\246\210.kt" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "src/main/kotlin/byeonghee/week26/\354\271\230\354\246\210.kt" diff --git "a/src/main/kotlin/byeonghee/week26/\354\271\230\354\246\210.kt" "b/src/main/kotlin/byeonghee/week26/\354\271\230\354\246\210.kt" new file mode 100644 index 00000000..4d65ae1c --- /dev/null +++ "b/src/main/kotlin/byeonghee/week26/\354\271\230\354\246\210.kt" @@ -0,0 +1,67 @@ +package byeonghee.week26 + +import java.io.StreamTokenizer + +const val AIR = 0 +const val CHEESE = 1 + +val dr = arrayOf(1, 0, -1, 0) +val dc = arrayOf(0, 1, 0, -1) + +fun main() = StreamTokenizer(System.`in`.bufferedReader()).run { + + fun input() : Int { + nextToken() + return nval.toInt() + } + + val h = input() + val w = input() + val plane = Array(h) { IntArray(w) } + + val q = ArrayList>() + var cheese = 0 + var record = 0 + var timeCost = 0 + + for(r in 0 until h) for(c in 0 until w) { + plane[r][c] = input() + if (plane[r][c] == CHEESE) cheese++ + } + + var r = 0 + var c = 0 + var nr = 0 + var nc = 0 + while(cheese > 0) { + val visited = Array(h) { BooleanArray(w) } + record = cheese + q.add(Pair(0, 0)) + visited[0][0] = true + + while(q.isNotEmpty()) { + q.removeFirst().let { + r = it.first + c = it.second + } + + for(d in 0 until 4) { + nr = r + dr[d] + nc = c + dc[d] + + if (nr !in 0 until h || nc !in 0 until w) continue + if (visited[nr][nc]) continue + + if (plane[nr][nc] == CHEESE) { + plane[nr][nc] = AIR + cheese-- + } + else q.add(Pair(nr, nc)) + visited[nr][nc] = true + } + } + timeCost++ + } + + println("$timeCost\n$record") +} \ No newline at end of file From 4b5dbde976e4ac0fb85833edcb9ee324ca6434a1 Mon Sep 17 00:00:00 2001 From: bngsh Date: Thu, 9 Mar 2023 14:23:19 +0900 Subject: [PATCH 4/5] =?UTF-8?q?chore:=20=ED=85=9C=ED=94=8C=EB=A6=BF=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\353\214\223\352\260\222 \355\236\231.kt" | 27 +++-- .../week26/\354\271\230\354\246\210.kt" | 101 ++++++++++-------- 2 files changed, 73 insertions(+), 55 deletions(-) diff --git "a/src/main/kotlin/byeonghee/week26/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" "b/src/main/kotlin/byeonghee/week26/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" index ad2705e3..ac54d2e3 100644 --- "a/src/main/kotlin/byeonghee/week26/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" +++ "b/src/main/kotlin/byeonghee/week26/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" @@ -4,16 +4,25 @@ import java.io.* import java.util.PriorityQueue import kotlin.math.abs -fun main(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) { - val n = readLine().toInt() - val pq = PriorityQueue { a, b -> if (a + b == 0) a - b else abs(a) - abs(b) } +class 소병희_절댓값힙 { - var x : Int - repeat(n) { - x = readLine().toInt() - when(x) { - 0 -> println(pq.poll() ?: "0") - else -> pq.add(x) + companion object { + fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) { + val n = readLine().toInt() + val pq = PriorityQueue { a, b -> if (a + b == 0) a - b else abs(a) - abs(b) } + + var x : Int + repeat(n) { + x = readLine().toInt() + when(x) { + 0 -> println(pq.poll() ?: "0") + else -> pq.add(x) + } + } } } +} + +fun main() { + 소병희_절댓값힙.solve() } \ No newline at end of file diff --git "a/src/main/kotlin/byeonghee/week26/\354\271\230\354\246\210.kt" "b/src/main/kotlin/byeonghee/week26/\354\271\230\354\246\210.kt" index 4d65ae1c..184edd5e 100644 --- "a/src/main/kotlin/byeonghee/week26/\354\271\230\354\246\210.kt" +++ "b/src/main/kotlin/byeonghee/week26/\354\271\230\354\246\210.kt" @@ -2,66 +2,75 @@ package byeonghee.week26 import java.io.StreamTokenizer -const val AIR = 0 -const val CHEESE = 1 +class 소병희_치즈 { -val dr = arrayOf(1, 0, -1, 0) -val dc = arrayOf(0, 1, 0, -1) + companion object { + const val AIR = 0 + const val CHEESE = 1 -fun main() = StreamTokenizer(System.`in`.bufferedReader()).run { + val dr = arrayOf(1, 0, -1, 0) + val dc = arrayOf(0, 1, 0, -1) - fun input() : Int { - nextToken() - return nval.toInt() - } + fun solve() = StreamTokenizer(System.`in`.bufferedReader()).run { - val h = input() - val w = input() - val plane = Array(h) { IntArray(w) } + fun input() : Int { + nextToken() + return nval.toInt() + } - val q = ArrayList>() - var cheese = 0 - var record = 0 - var timeCost = 0 + val h = input() + val w = input() + val plane = Array(h) { IntArray(w) } - for(r in 0 until h) for(c in 0 until w) { - plane[r][c] = input() - if (plane[r][c] == CHEESE) cheese++ - } + val q = ArrayList>() + var cheese = 0 + var record = 0 + var timeCost = 0 - var r = 0 - var c = 0 - var nr = 0 - var nc = 0 - while(cheese > 0) { - val visited = Array(h) { BooleanArray(w) } - record = cheese - q.add(Pair(0, 0)) - visited[0][0] = true - - while(q.isNotEmpty()) { - q.removeFirst().let { - r = it.first - c = it.second + for(r in 0 until h) for(c in 0 until w) { + plane[r][c] = input() + if (plane[r][c] == CHEESE) cheese++ } - for(d in 0 until 4) { - nr = r + dr[d] - nc = c + dc[d] + var r = 0 + var c = 0 + var nr = 0 + var nc = 0 + while(cheese > 0) { + val visited = Array(h) { BooleanArray(w) } + record = cheese + q.add(Pair(0, 0)) + visited[0][0] = true - if (nr !in 0 until h || nc !in 0 until w) continue - if (visited[nr][nc]) continue + while(q.isNotEmpty()) { + q.removeFirst().let { + r = it.first + c = it.second + } - if (plane[nr][nc] == CHEESE) { - plane[nr][nc] = AIR - cheese-- + for(d in 0 until 4) { + nr = r + dr[d] + nc = c + dc[d] + + if (nr !in 0 until h || nc !in 0 until w) continue + if (visited[nr][nc]) continue + + if (plane[nr][nc] == CHEESE) { + plane[nr][nc] = AIR + cheese-- + } + else q.add(Pair(nr, nc)) + visited[nr][nc] = true + } } - else q.add(Pair(nr, nc)) - visited[nr][nc] = true + timeCost++ } + + println("$timeCost\n$record") } - timeCost++ } +} - println("$timeCost\n$record") +fun main() { + 소병희_치즈.solve() } \ No newline at end of file From 1305c256de27ab3c8446239094c0216428dc0caf Mon Sep 17 00:00:00 2001 From: bngsh Date: Thu, 9 Mar 2023 15:55:04 +0900 Subject: [PATCH 5/5] =?UTF-8?q?solve:=20=EC=B9=98=ED=82=A8=20=EB=B0=B0?= =?UTF-8?q?=EB=8B=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\355\202\250 \353\260\260\353\213\254.kt" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "src/main/kotlin/byeonghee/week26/\354\271\230\355\202\250 \353\260\260\353\213\254.kt" diff --git "a/src/main/kotlin/byeonghee/week26/\354\271\230\355\202\250 \353\260\260\353\213\254.kt" "b/src/main/kotlin/byeonghee/week26/\354\271\230\355\202\250 \353\260\260\353\213\254.kt" new file mode 100644 index 00000000..aba96cf7 --- /dev/null +++ "b/src/main/kotlin/byeonghee/week26/\354\271\230\355\202\250 \353\260\260\353\213\254.kt" @@ -0,0 +1,68 @@ +package byeonghee.week26 + +import java.io.StreamTokenizer +import kotlin.math.abs + +class 소병희_치킨배달 { + + companion object { + fun solve() = StreamTokenizer(System.`in`.bufferedReader()).run { + + fun input() : Int { + nextToken() + return nval.toInt() + } + + val n = input() + val m = input() + val house = mutableListOf>() + val franchise = mutableListOf>() + var cityDist = Integer.MAX_VALUE + + for(r in 0 until n) for(c in 0 until n) { + when(input()) { + 1 -> house.add(Pair(r, c)) + 2 -> franchise.add(Pair(r, c)) + } + } + + val houseCnt = house.size + val franchiseCnt = franchise.size + val distMap = Array(houseCnt) { h -> IntArray(franchiseCnt) { f -> house[h].calcDist(franchise[f]) } } + + fun checkCityDist(chickenDist: IntArray) { + var ret = 0 + for(h in 0 until houseCnt) { + ret += chickenDist[h] + } + cityDist = cityDist.coerceAtMost(ret) + } + + fun calcNewDist(f: Int, oldDist: IntArray) : IntArray { + return IntArray(houseCnt) { h -> oldDist[h].coerceAtMost(distMap[h][f]) } + } + + fun makeM(f: Int, i: Int, chickenDist: IntArray) { + if (f == franchiseCnt && i < m) return + if (i == m) { + checkCityDist(chickenDist) + return + } + + makeM(f + 1, i + 1, calcNewDist(f, chickenDist)) + if(franchiseCnt - f >= m - i) makeM(f + 1, i, chickenDist) + } + + makeM(0, 0, IntArray(houseCnt) { n * n }) + print(cityDist) + } + + fun Pair.calcDist(p: Pair) : Int { + return abs(first - p.first) + abs(second - p.second) + } + } +} + +fun main() { + 소병희_치킨배달.solve() +} \ No newline at end of file