Skip to content

Commit

Permalink
Merge pull request #192 from wellFoundedDevelopers/byeonghee/48week
Browse files Browse the repository at this point in the history
[소병희] 백도어, 탑, 주차장
  • Loading branch information
bngsh authored Oct 15, 2023
2 parents c5ee54c + 4e582e8 commit 3b70416
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
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()
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>()
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) {
val space = parked[car * -1]
parked[car * -1] = -1
spaceQ.add(space)
ans += fee[space] * weight[car * -1]
}
else waitQ.add(car)

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
}

st.addFirst(reach)
}

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

0 comments on commit 3b70416

Please sign in to comment.