diff --git "a/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" "b/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" new file mode 100644 index 0000000..d77659b --- /dev/null +++ "b/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" @@ -0,0 +1,50 @@ +package `4week`.acw + +class `acw근손실` { + lateinit var exerciseKits: List<Int> + lateinit var visit: Array<Boolean> + var cnt = 0 + + + fun makePermutation(arr: MutableList<Int>, power: Int, day: Int, N: Int, K: Int) { + //순열구성하기 + val powerNow = power + (arr.takeIf { it.isNotEmpty() }?.let { it.last() - K } ?: 0) + + if (powerNow < 500) { + return + } + + if (day == N) { + cnt++ + return + } + + for (i in 0 until N) { + if (visit[i]) { + continue + } + + arr.add(exerciseKits[i]) + visit[i] = true + makePermutation(arr, powerNow, day + 1, N, K) + arr.remove(exerciseKits[i]) + visit[i] = false + } + } + + + fun solution() { + val (N, K) = readln().split(" ").map { it.toInt() } + exerciseKits = readln().split(" ").map { it.toInt() } + visit = Array(N) { false } + makePermutation(mutableListOf(), 500, 0, N, K) + + println(cnt) + + } +} + +fun main() { + val sol = `acw근손실`() + sol.solution() +} \ No newline at end of file diff --git "a/src/4week/acw/\354\243\274\354\260\250\354\232\224\352\270\210\352\263\204\354\202\260.kt" "b/src/4week/acw/\354\243\274\354\260\250\354\232\224\352\270\210\352\263\204\354\202\260.kt" new file mode 100644 index 0000000..03bde37 --- /dev/null +++ "b/src/4week/acw/\354\243\274\354\260\250\354\232\224\352\270\210\352\263\204\354\202\260.kt" @@ -0,0 +1,85 @@ +import java.util.LinkedList + +class Solution { + + val recordsArr = Array(10000) { LinkedList<String>() } + val feeArr = Array(10000) { 0 } + + data class Time(var hour: Int, var min: Int) { + + constructor(hourAndMin: List<Int>) : this(hourAndMin[0], hourAndMin[1]) { + this.hour = hourAndMin[0] + this.min = hourAndMin[1] + } + + companion object { + fun calcUsingTime(inTime: Time, outTime: Time): Int { + return (outTime.hour * 60 + outTime.min - inTime.hour * 60 - inTime.min) + } + } + } + + fun String.timeSplit() = split(":").map { it.toInt() } + + fun solution(fees: IntArray, records: Array<String>): IntArray { + var answer: IntArray = intArrayOf() + + for (i in records) { + val (time, num, inout) = i.split(" ") + if (inout == "IN") { + recordsArr[num.toInt()].add(time) + } else { + //out이 나올경우에는 바로 해당번호에 대해 처리를 해준다. + val inTime = Time((recordsArr[num.toInt()].removeLast().timeSplit())) + val outTime = Time(time.timeSplit()) + + val usingTime = Time.calcUsingTime(inTime, outTime) + feeArr[num.toInt()] += usingTime + } + } + + for (i in 0 until 10000) { + while (recordsArr[i].isNotEmpty()) { + val now = Time(recordsArr[i].removeFirst().timeSplit()) + val usingTime = Time.calcUsingTime(Time(23, 59), now) + feeArr[i] += usingTime + } + }//In은 했는데 Out은 안한경우 위에서 처리가 안되므로 처음부터 돌면서 23:59에서 빼준다. + + + //요금 계산하기 + val arr = mutableListOf<Int>() + for (i in 0 until 10000) { + + if (feeArr[i] == 0) { + continue + } else { + + if (feeArr[i] <= fees[0]) { + arr.add(fees[1]) + //기본요금 부과 + } else { + val ceilCheck = (feeArr[i] - fees[0]) % fees[2] != 0// + //올림하라했으므로 올림해야하는지 check해주기 + + var remain = if (ceilCheck) { + (feeArr[i] - fees[0]) / fees[2] + 1 + } else { + (feeArr[i] - fees[0]) / fees[2] + } + + + arr.add(fees[1] + remain * fees[3]) + + } + } + + } + answer = arr.toIntArray() + + + return answer + } + + +} \ No newline at end of file diff --git "a/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" "b/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" new file mode 100644 index 0000000..a84e21f --- /dev/null +++ "b/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" @@ -0,0 +1,58 @@ +package `4week`.acw + + +import java.util.* + +class `acw지름길` { + lateinit var distance: Array<Int> + var roads = PriorityQueue(Comparator<Triple<Int, Int, Int>> { a, b -> + a.first - b.first + }) + + + fun solution() { + val (K, D) = readln().split(" ").map { it.toInt() } + distance = Array(10001) { it } + + + for (i in 1..K) { + val (s, e, c) = readln().split(" ").map { it.toInt() } + + if (e > D) { + continue + } + //목표보다 높은 지점을 향하는 지름길은 받지 않기 + + if (c >= (e - s)) { + continue + } + //지름길이 cost가 더 클경우도 받지 않기 + + roads.add(Triple(s, e, c)) + } + + while (roads.isNotEmpty()) { + val (s, e, c) = roads.poll() + if (distance[e] - distance[s] > c) { + distance[e] = distance[s] + c + for (j in e + 1..D) { + if (distance[j - 1] + 1 < distance[j]) { + distance[j] = distance[j - 1] + 1 + } + } + // 지름길을 순회하며 더 최소값일 경우 그 뒤의 node들에 대해서 값 갱신 + } + } + + + + println(distance[D]) + + + } +} + +fun main() { + val sol = `acw지름길`() + sol.solution() +} diff --git "a/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" "b/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" new file mode 100644 index 0000000..10cd201 --- /dev/null +++ "b/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" @@ -0,0 +1,120 @@ +package `4week`.acw + + +import kotlin.math.pow + +//시계방향은 오른쪽shift 반시계는 왼쪽 shift +const val RIGHT = 5 +const val LEFT = 1 + +class `acw톱니바퀴` { + + var wheel = Array(4) { 0 } + var visit = Array(4) { false } + var turn = arrayListOf<Pair<Int, Int>>() + + + private fun addTurn(w: Int, turnD: Int) { + //dfs로 들어가면서 회전해야할 것들 turn에 추가해주기 + + + val wheelNowRight = if (wheel[w] and (1 shl RIGHT) != 0) 1 else 0 + val wheelNowLeft = if (wheel[w] and (1 shl LEFT) != 0) 1 else 0 + + if (w - 1 >= 0) { + val leftWheelsRight = if (wheel[w - 1] and (1 shl RIGHT) != 0) 1 else 0 + if (wheelNowLeft != leftWheelsRight && !visit[w - 1]) { + visit[w - 1] = true + turn.add(Pair(w - 1, turnD * -1)) + addTurn(w - 1, turnD * -1) + } + } + + if (w + 1 < 4) { + val rightWheelsLeft = if (wheel[w + 1] and (1 shl LEFT) != 0) 1 else 0 + if (wheelNowRight != rightWheelsLeft && !visit[w + 1]) { + visit[w + 1] = true + turn.add(Pair(w + 1, turnD * -1)) + addTurn(w + 1, turnD * -1) + } + } + + + } + + private fun turnWheel() { + + while (turn.isNotEmpty()) { + val (w, d) = turn.removeFirst() + + if (d == -1) { + //반시계 + val last = wheel[w] and (1 shl 7) + + wheel[w] = (wheel[w] shl 1) and 255 + if (last != 0) { + wheel[w]++ + } + + } else { + //시계 + val first = wheel[w] and 1 + + wheel[w] = wheel[w] shr 1 + + if (first != 0) { + wheel[w] += 1 shl 7 + } + } + + + } + + } + + + fun solution() { + repeat(4) { + var input = readln() + var m = 128 + var num = 0 + for (i in input) { + num += (i.digitToInt() * m) + m /= 2 + }//숫자로 바꿔서 저장 + wheel[it] = num + + } + + val turnCnt = readln().toInt() + repeat(turnCnt) { + val (w, d) = readln().split(" ").map { it.toInt() } + turn.add(Pair(w - 1, d)) + visit[w - 1] = true + + + addTurn(w - 1, d)//회전해야할 것들 turn List에 추가해주기 + turnWheel()// turn List에서 빼서 회전시켜주기 + visit = Array(4) { false } // visit은 다시 사용해야함으로 다시 false로 초기화 + } + + + val answer = wheel.withIndex().sumOf { (idx, value) -> + if (value and (1 shl 7) != 0) { + (2.0).pow(idx).toInt() + } else { + 0 + } + } + + + println(answer) + + + } +} + +fun main() { + val sol = `acw톱니바퀴`() + sol.solution() +} diff --git "a/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" "b/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" new file mode 100644 index 0000000..11461ae --- /dev/null +++ "b/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" @@ -0,0 +1,40 @@ +package `4week`.acw + +class `acw트리의부모찾기` { + lateinit var treeMap: Array<MutableSet<Int>> + lateinit var parent: Array<Int> + var N = 0 + + fun dfs(p: Int) { + //p로 전달받은 노드는 부모란 뜻이니까 연결된 노드들은 자식으로 처리해주기 + //dfs로 이어가면서 같은 처리 해주기 + for (child in treeMap[p]) { + parent[child] = p + treeMap[child].remove(p) + dfs(child) + } + } + + + fun solution() { + N = readln().toInt() + treeMap = Array(N + 1) { mutableSetOf() } + parent = Array(N + 1) { 0 } + + for (i in 1 until N) { + val (a, b) = readln().split(" ").map { it.toInt() } + treeMap[a].add(b) + treeMap[b].add(a) + //일단 입력받을 경우 두 node에 대해 상호 연결하기 + } + + dfs(1) + println(parent.drop(2).joinToString("\n")) + + } +} + +fun main() { + val sol = `acw트리의부모찾기`() + sol.solution() +} diff --git "a/src/4week/acw/\355\232\214\354\235\230\354\213\244\353\260\260\354\240\225.kt" "b/src/4week/acw/\355\232\214\354\235\230\354\213\244\353\260\260\354\240\225.kt" new file mode 100644 index 0000000..3455c5d --- /dev/null +++ "b/src/4week/acw/\355\232\214\354\235\230\354\213\244\353\260\260\354\240\225.kt" @@ -0,0 +1,46 @@ +package `4week`.acw + +import java.util.PriorityQueue + + +class `acw회의실배정` { + val conferance = PriorityQueue( + compareBy<Pair<Int, Int>> {it.second}.thenBy { it.first } + ) +// 종료시간이 빠른것을 기준으로 정렬한다. +// 종료시간이 같을 경우 시작시간이 더 빠른것을 위주로 정렬해야한다. -> 시작시간과 종료시간이 같은 회의가 있을 수 있기 때문 +// ex) 5,6 / 6,6 이 있는데 6,6이 먼저 뽑힐 경우 56 /66이 모두 사용될 수 있음에도 불구하고 56은 선택되지 못한다. + + fun solution() { + val N = readln().toInt() + var answer = 1 + var lastConferance = Pair(0, 0) + + repeat(N) { + val (s, e) = readln().split(" ").map { it.toInt() } + conferance.add(Pair(s, e)) + + } + lastConferance = conferance.poll() + + + while (conferance.isNotEmpty()) { + val nowConferance = conferance.poll() + if (nowConferance.first >= lastConferance.second) { + lastConferance = nowConferance + answer++ + } + + } + println(answer) + + + } + +} + +fun main() { + val sol = `acw회의실배정`() + sol.solution() +} +