Skip to content
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

[소병희] 백도어, 탑, 주차장 #192

Merged
merged 4 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/main/kotlin/byeonghee/week48/백도어.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package byeonghee.week48

import java.util.*

class 소병희_백도어 {

companion object {
const val INF = Long.MAX_VALUE

fun solve()= with(System.`in`.bufferedReader()) {
val (n, m) = readLine().split(" ").map { it.toInt() }
val hideable = readLine().split(" ").map { it == "0" }.toBooleanArray()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"0"과의 일치여부로 true, false 로 만들 생각을 하시다니 대단해요

val adj = Array(n) { ArrayDeque<Pair<Int, Long>>() }

repeat(m) {
val (a, b, t) = readLine().split(" ").map { it.toInt() }
adj[a].add(b to t.toLong())
adj[b].add(a to t.toLong())
}

val visited = LongArray(n) { INF }
val q = PriorityQueue<Pair<Int, Long>> { a, b -> (a.second - b.second).toInt() }
val GOAL = n-1

visited[0] = 0L
q.add(0 to 0L)

while(q.isNotEmpty()) {
val (pos, curDist) = q.poll()

if (visited[pos] < curDist) continue
visited[pos] = curDist

for((v, e) in adj[pos]) {
if (v != GOAL && hideable[v].not()) continue
if (curDist + e < visited[v]) {
visited[v] = curDist + e
q.add(v to visited[v])
}
}
}

println(if (visited[GOAL] == INF) -1 else visited[GOAL])
}
}
}

fun main() {
소병희_백도어.solve()
}
44 changes: 44 additions & 0 deletions src/main/kotlin/byeonghee/week48/주차장.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package byeonghee.week48

import java.util.PriorityQueue
import kotlin.collections.ArrayDeque

class 소병희_주차장 {

companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val (n, m) = readLine().split(" ").map { it.toInt()}
val fee = IntArray(n)
val weight = IntArray(m + 1)
val parked = IntArray(m + 1) { -1 }
val spaceQ = PriorityQueue<Int>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaceQ가 있으면 비어있는 빠른 인덱스을 찾는 시간이 안걸리겠네요!!

val waitQ = ArrayDeque<Int>()
var ans = 0

repeat(n) { i -> fee[i] = readLine().toInt() }
repeat(m) { i -> weight[i+1] = readLine().toInt() }

spaceQ.addAll(0 until n)
repeat(2*m) {
val car = readLine().toInt()

if (car < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

빼내는 처리 먼저 하니 코드가 간결해지네요!😲

val space = parked[car * -1]
parked[car * -1] = -1
spaceQ.add(space)
ans += fee[space] * weight[car * -1]
}
else waitQ.add(car)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

무조건 q에 넣는거 신박하네용 😮


if (spaceQ.isNotEmpty() && waitQ.isNotEmpty()) {
val space = spaceQ.poll()
val car = waitQ.removeFirst()

parked[car] = space
}
}

println(ans)
}
}
}
28 changes: 28 additions & 0 deletions src/main/kotlin/byeonghee/week48/탑.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package byeonghee.week48

class 소병희_탑 {

companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val n = readLine().toInt()
val towers = IntArray(n)
val st = ArrayDeque<Int>()
val answer = IntArray(n)


readLine().split(" ").forEachIndexed { i, v ->
towers[i] = v.toInt()
}

for(reach in n-1 downTo 0) {
while (st.isNotEmpty() && towers[reach] > towers[st.first()]) {
answer[st.removeFirst()] = reach + 1
}
Comment on lines +18 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요렇게 While문 하나로도 깔끔하게 처리할 수 있군요


st.addFirst(reach)
}

println(answer.joinToString(" "))
}
}
}