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

[소병희] 알고스팟, A와 B 2, 회장뽑기, 입국심사 #188

Merged
merged 4 commits into from
Oct 8, 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
37 changes: 37 additions & 0 deletions src/main/kotlin/byeonghee/week47/A와 B 2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package byeonghee.week47

class 소병희_A와B2 {

companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val s = readLine()
val t = readLine()

fun checkAB(sb: StringBuilder): Boolean {
Copy link
Member

Choose a reason for hiding this comment

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

오... 함수안에 함수를 선언할 수도 있군요??...

if (s == sb.toString()) return true
if (s.length == sb.length) return false
Comment on lines +11 to +12
Copy link
Member

Choose a reason for hiding this comment

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

오 깔끔해용

Comment on lines +11 to +12
Copy link
Member

Choose a reason for hiding this comment

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

이렇게 if 두 개로 판단하는 게 좋았슴다!


if (sb.last() == 'A') {
val nsb = StringBuilder(sb)
nsb.deleteCharAt(nsb.lastIndex)
if (checkAB(nsb)) return true
}
if (sb.first() == 'B') {
val nsb = StringBuilder(sb)
nsb.deleteCharAt(0)
nsb.reverse()
if (checkAB(nsb)) return true
}

return false
}

val sb = StringBuilder(t)
println(if (checkAB(sb)) 1 else 0)
}
}
}

fun main() {
소병희_A와B2.solve()
}
53 changes: 53 additions & 0 deletions src/main/kotlin/byeonghee/week47/알고스팟.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package byeonghee.week47

import java.util.*

class 소병희_알고스팟 {

companion object {
const val INF = 99_999
const val WALL = 1
const val ROOM = 0

val dr = intArrayOf(0, 1, 0, -1)
val dc = intArrayOf(1, 0, -1, 0)

fun solve() = with(System.`in`.bufferedReader()) {
val (n, m) = readLine().split(" ").map { it.toInt() }
val maze = Array(m) { IntArray(n) }
val dist = Array(m) { IntArray(n) { INF } }
val pq = PriorityQueue<IntArray> { a, b -> a[0] - b[0] }
Copy link
Member

Choose a reason for hiding this comment

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

pq를 이용한 bfs는 0-1bfs랑 같은 효과가 있군요 !


repeat(m) { i ->
readLine().forEachIndexed { j, v ->
maze[i][j] = v.digitToInt()
}
}

pq.add(intArrayOf(0, 0, 0))
while(pq.isNotEmpty()) {
val (d, r, c) = pq.poll()
if (dist[r][c] <= d) continue
dist[r][c] = d

for(dir in 0 until 4) {
val nr = r + dr[dir]
val nc = c + dc[dir]
if (nr !in maze.indices || nc !in maze[0].indices) continue
if (maze[nr][nc] == ROOM) {
pq.add(intArrayOf(d, nr, nc))
}
else if (maze[nr][nc] == WALL) {
pq.add(intArrayOf(d+1, nr, nc))
}
Comment on lines +37 to +42
Copy link
Member

Choose a reason for hiding this comment

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

우선순위를 요렇게 이용할 수도 있군요

}
}

println(dist[m-1][n-1])
}
}
}

fun main() {
소병희_알고스팟.solve()
}
20 changes: 20 additions & 0 deletions src/main/kotlin/byeonghee/week47/입국심사.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package byeonghee.week47

class 소병희_입국심사 {
fun solution(n: Int, times: IntArray): Long {
times.sort()

var lb = 0L
var ub = times.last() * n.toLong()
var mid = 0L

while(lb + 1 < ub) {
mid = lb + (ub - lb) / 2
val cost = times.sumOf { mid / it }
if (cost < n) lb = mid
else ub = mid
}
Comment on lines +11 to +16
Copy link
Member

Choose a reason for hiding this comment

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

굿굿👍👍
동작 과정을 다시 봐야겠슴다!


return ub
}
}
61 changes: 61 additions & 0 deletions src/main/kotlin/byeonghee/week47/회장뽑기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package byeonghee.week47

class 소병희_회장뽑기 {

companion object {
const val INF = 987_654_321

fun solve() = with(System.`in`.bufferedReader()) {
val n = readLine().toInt()
val adj = Array(n) { i -> IntArray(n) { j -> if (i == j) 0 else INF }}

var a = 0
var b = 0

readLine().split(" "). let {
a = it[0].toInt()
b = it[1].toInt()
}

while(a != -1) {
adj[a-1][b-1] = 1
adj[b-1][a-1] = 1

readLine().split(" "). let {
a = it[0].toInt()
b = it[1].toInt()
}
}

for(k in 0 until n) {
for(i in 0 until n) for(j in 0 until n) {
val before = adj[i][j]
val detour = adj[i][k] + adj[k][j]
if (detour < before) adj[i][j] = detour
}
}
Comment on lines +30 to +36
Copy link
Member

Choose a reason for hiding this comment

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

헉 플로이드 워셜...


var minScore = 987_654_321
val candidates = ArrayDeque<Int>()

for(i in 0 until n) {
val score = adj[i].maxOf{it}
if (score < minScore) {
minScore = score
candidates.clear()
candidates.add(i + 1)
}
else if (score == minScore) {
candidates.add(i + 1)
}
}
Comment on lines +41 to +51
Copy link
Member

Choose a reason for hiding this comment

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

오 이렇게 구하는거 좋네요


println("$minScore ${candidates.size}")
println(candidates.joinToString(" "))
}
}
}

fun main() {
소병희_회장뽑기.solve()
}