-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[소병희] 표현 가능한 이진트리, 절댓값 힙, 치즈, 치킨 배달 #112
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package byeonghee.week26 | ||
|
||
import java.io.* | ||
import java.util.PriorityQueue | ||
import kotlin.math.abs | ||
|
||
class 소병희_절댓값힙 { | ||
|
||
companion object { | ||
fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) { | ||
val n = readLine().toInt() | ||
val pq = PriorityQueue<Int> { 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() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package byeonghee.week26 | ||
|
||
import java.io.StreamTokenizer | ||
|
||
class 소병희_치즈 { | ||
|
||
companion object { | ||
const val AIR = 0 | ||
const val CHEESE = 1 | ||
|
||
val dr = arrayOf(1, 0, -1, 0) | ||
val dc = arrayOf(0, 1, 0, -1) | ||
|
||
fun solve() = 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<Pair<Int, Int>>() | ||
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") | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
소병희_치즈.solve() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Pair<Int, Int>>() | ||
val franchise = mutableListOf<Pair<Int, Int>>() | ||
var cityDist = Integer.MAX_VALUE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Int의 상수 대신 Interger의 상수를 사용하신 이유가 있으신가요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ㅇㅁㅇ 아뇨 자바랑 헷갈렸나봐요 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ |
||
|
||
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]) } } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 거리를 미리 저장하시다니!👍 |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 조건처리가 좋은 것 같습니다..!!! |
||
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<Int, Int>.calcDist(p: Pair<Int, Int>) : Int { | ||
return abs(first - p.first) + abs(second - p.second) | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
소병희_치킨배달.solve() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Long도 toBinaryString이 있군요... |
||
|
||
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) | ||
} | ||
Comment on lines
+34
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 재귀 장인이시군요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인졍..! |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pq를 굉장히 아름답게 쓰시네요,, 항상 배우고 갑니당