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

[Week4] 소병희: 근손실, 회의실 배정, 트리의 부모 찾기, 주차 요금 계산 #22

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions src/4week/byeonghee/근손실.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package `4week`.byeonghee

import java.io.*

class `소병희_근손실` {
val br = BufferedReader(InputStreamReader(System.`in`))

var n = 0
var k = 0
var workouts = IntArray(0)

val visited = BooleanArray(8) { false }
var curPerm = 0
var answer = 0

fun solution() {
br.readLine().split(" ").map { it.toInt() }.run {
n = first()
k = last()
}
workouts = br.readLine().split(" ").map { it.toInt() - k }.toIntArray()

makePerm(0)
println(answer)
}

fun makePerm(len: Int) {
if (len == n) {
answer++
return
}

for(i in 0 until n) {
if (visited[i]) continue
if (curPerm + workouts[i] < 0) continue

visited[i] = true
curPerm += workouts[i]
makePerm(len + 1)
curPerm -= workouts[i]
visited[i] = false
}
}
}

fun main() {
`소병희_근손실`().solution()
}
49 changes: 49 additions & 0 deletions src/4week/byeonghee/주차 요금 계산.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package `4week`.byeonghee

class `소병희_주차 요금 계산` {

companion object {

const val BASE_TIME = 0
const val BASE_FEE = 1
const val UNIT_TIME = 2
const val UNIT_FEE = 3

fun String.toMinutes() = this.split(":").map{ it.toInt() }.let{ it.first() * 60 + it.last() }
}

val closingT = "23:59".toMinutes()
val checkMap = hashMapOf<String, Int>()
val timeMap = hashMapOf<String, Int>()
lateinit var answer: IntArray

fun solution(fees: IntArray, records: Array<String>): IntArray {
for((t, c, s) in records.map{ it.split(" ")}.sortedBy{ it.first() }) {
when (s) {
"IN" -> checkMap.put(c, t.toMinutes())
"OUT" -> {
timeMap.put(c, timeMap.getOrDefault(c, 0) + t.toMinutes() - checkMap[c]!!)
checkMap.put(c, -1)
}
}
}

answer = IntArray(checkMap.keys.size)
var fee = 0
var i = 0
for(c in checkMap.keys.sorted()) {
if (checkMap[c]!! != -1) {
timeMap.put(c, timeMap.getOrDefault(c, 0) + closingT - checkMap[c]!!)
}

fee = fees[BASE_FEE]
fee += Integer.max(0, timeMap[c]!! - fees[BASE_TIME]).let {
(it / fees[UNIT_TIME]) + (if (it % fees[UNIT_TIME] == 0) 0 else 1)
} * fees[UNIT_FEE]

answer[i++] = fee
}

return answer
}
}
56 changes: 56 additions & 0 deletions src/4week/byeonghee/지름길.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package `4week`.byeonghee

import java.io.*

val br = BufferedReader(InputStreamReader(System.`in`))

var answer = Int.MAX_VALUE
var dist = 0
var D = 0
var N = 0

data class Edge(val s: Int, val e: Int, val d: Int)
val edgeMap = mutableMapOf<Int, MutableList<Edge>>()

fun main() {
br.readLine().split(" ").map{ it.toInt() }.let {
N = it[0]
D = it[1]
}

repeat(N) {
val (s, e, d) = br.readLine().split(" ").map{ it.toInt() }
if ((e <= D) && (d < (e - s))) {
if (edgeMap.contains(s)) {
edgeMap[s]!!.add(Edge(s, e, d))
}
else {
edgeMap[s] = mutableListOf(Edge(s, e, d))
}
}
}

dfs(0)
println(answer)
}

fun dfs(node: Int) {
println(node)

var nxt = node
var mv = 0
while(edgeMap.containsKey(nxt).not() && nxt < D) {
nxt++
mv++
}
if (nxt == D) {
answer = Integer.min(answer, dist + mv)
return
}
println(nxt)
for(i in edgeMap[nxt]!!) {
dist += i.d + mv
dfs(i.e)
dist -= i.d - mv
}
}
42 changes: 42 additions & 0 deletions src/4week/byeonghee/트리의 부모 찾기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package `4week`.byeonghee

import java.io.*

class `트리의 부모 찾기` {
val br = BufferedReader(InputStreamReader(System.`in`))

lateinit var parents : IntArray
val edges = mutableMapOf<Int, MutableList<Int>>()
val q = ArrayDeque<Pair<Int, Int>>()

fun solution() {
val n = br.readLine().toInt()
parents = IntArray(n + 1)

repeat(n-1) { i ->
br.readLine().split(" ").map{ it.toInt() }.run {
edges.getOrPut(first()) { mutableListOf() }
edges[first()]!!.add(last())
edges.getOrPut(last()) { mutableListOf() }
edges[last()]!!.add(first())
if (first() == 1) q.add(Pair(first(), last()))
else if (last() == 1) q.add(Pair(last(), first()))
}
}

lateinit var cur: Pair<Int, Int>
while(q.isNotEmpty()) {
cur = q.removeFirst()
parents[cur.second] = cur.first
for(i in edges[cur.second]!!) {
if (parents[i] == 0) q.add(Pair(cur.second, i))
}
}

println(parents.drop(2).joinToString("\n"))
}
}

fun main() {
`트리의 부모 찾기`().solution()
}
39 changes: 39 additions & 0 deletions src/4week/byeonghee/회의실 배정.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package `4week`.byeonghee

import java.io.*
import java.util.PriorityQueue

class `소병희_회의실 배정` {

data class Meeting(val s: Int, val e: Int)

val br = BufferedReader(InputStreamReader(System.`in`))
val meetings = PriorityQueue(Comparator<Meeting> { a, b -> if (a.e == b.e) a.s - b.s else a.e - b.e })

fun solution() {
val n = br.readLine().toInt()
repeat(n) {
br.readLine().split(" ").map{ it.toInt() }.run{
meetings.add(Meeting(first(), last()))
}
}

var answer = 0
var curLastT = 0
lateinit var nxt : Meeting

while(meetings.isNotEmpty()) {
nxt = meetings.poll()
if (curLastT <= nxt.s) {
curLastT = nxt.e
answer++
}
}

println(answer)
}
}

fun main() {
`소병희_회의실 배정`().solution()
}