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] 전현수: 근손실, 톱니바퀴, 트리의 부모 찾기, 회의실 배정, 지름길 #18

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

class 전현수_근손실{
/**
* <문제>
* [근손실](https://www.acmicpc.net/problem/18429)
*
* 하루가 지나면 중량이 K만큼 감소
* N개의 서로 다른 운동 키트를 가지고 있음.
* 하루에 1개씩 키트를 사용. 사용하면 중량이 즉시 증가
* 운동 키트를 사용하면서 항상 중량이 500이상이 되도록 해야함.
*
* 아이디어
* - 완탐해서 모든 조합을 구하고
* - 각 조합들로 운동할 경우 500이상이 되는지를 판정하자!
*
*/

val exerciseKitList = mutableListOf<Int>()
Copy link
Member

Choose a reason for hiding this comment

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

이런부분 lateinit 해주세요!!!!!!

var possibleCaseCnt = 0
val exerciseCase = mutableListOf<Int>()
val visited = BooleanArray(8) { false }

fun solution() {

val (exerciseCnt, downWeight) = readln().split(" ").map { it.toInt() }
val exerciseData = readln().split(" ").map { it.toInt() }
exerciseKitList.addAll(exerciseData)

checkAllCases(0, exerciseCnt, downWeight)

println(possibleCaseCnt)
}

fun checkAllCases(cnt: Int, depth: Int, downWeight: Int) {

if (cnt == depth) {

var curWeight = 500
exerciseCase.forEach { upWeight ->
curWeight += upWeight - downWeight
if (curWeight < 500) return
}
possibleCaseCnt++
return
}
for (index in 0 until exerciseKitList.size) {
Copy link
Member

Choose a reason for hiding this comment

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

이부분 exerciseKitList.forEachIndexed 했으면 어땠을까요?

if (visited[index]) continue

visited[index] = true
exerciseCase.add(exerciseKitList[index])
checkAllCases(cnt + 1, depth, downWeight)
visited[index] = false
exerciseCase.removeAt(exerciseCase.lastIndex)
}
}
}

fun main(){
val myClass = 전현수_근손실()
myClass.solution()
}
188 changes: 188 additions & 0 deletions src/4week/hyunsoo/톱니바퀴.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package `4week`.hyunsoo

/**
* <문제>
* [톱니바퀴](https://www.acmicpc.net/problem/14891)
*
* 톱니바퀴는 8개의 톱니를 가지고 있고, 4개가 주어짐
* - 회전에 대한 정보가 주어지면
* - 1 ~ 4번의 톱니 상태를 모두 확인하기
* - 지정된 톱니부터 하나씩 차례로 회전 시키기
*
*
*
*
* 입/출력
* - 1 ~ 4째 줄에 톱니바퀴의 상태가 주어짐
* - 상태는 8개의 정수로 주어지고 12시방향부터 시계방향임.
* - N극은 0, S극은 1
* - 5째 줄에는 회전 횟수가 주어짐.
* - 그 후 K개의 줄에는 회전시킨 방법이 주어짐.
* - 첫 번째 정수는 회전시킨 톱니바퀴의 번호, 두 번째 정수는 방향
* - 방향이 1이면 시계 방향, -1이면 반시계 방향
*/

class `전현수_톱니바퀴`{
data class RotateInfo(val gearNum: Int, val dir: Int)

class Gear(stateData: String) {

private val state = IntArray(8)

init {
stateData.forEachIndexed { index, charNum ->
state[index] = charNum.digitToInt()
}
}
Comment on lines +28 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.

class Gear(val state: IntArray) {
        constructor(str: String): this(str.map { it.digitToInt() }.toIntArray())


// 시계 방향으로 회전
private fun rotateClockWise() {
val tempState = state.copyOf(state.size)
state[0] = state[state.lastIndex]
tempState.dropLast(1).forEachIndexed { index, num ->
state[index + 1] = num
}
}

// 반시계 방향으로 회전
private fun rotateCounterClockWise() {
val tempState = state.copyOf(state.size)
state[state.lastIndex] = state[0]
tempState.drop(1).forEachIndexed { index, num ->
state[index] = num
}
}

fun rotate(dir: Int) {
if (dir == 1) rotateClockWise()
else rotateCounterClockWise()
}

private fun getLeftPole() = state[6]

private fun getRightPole() = state[2]

fun getPole() = listOf(
getLeftPole(),
getRightPole()
)

fun getTopState() = state.first()

override fun toString(): String {
return this.state.joinToString("")
}
}


val gearList = Array(4) {
Gear(readln())
}

val affectedInfo = Array(4) {
mutableListOf<Int>()
}

fun solution() {

val gear1 = gearList.first()
val gear2 = gearList.second()
val gear3 = gearList.third()
val gear4 = gearList.fourth()
Comment on lines +88 to +91
Copy link
Member

Choose a reason for hiding this comment

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

@soopeach 이건 어떠세요?

val (gear1, gear2, gear3, gear4) = gearList

Copy link
Member Author

Choose a reason for hiding this comment

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

이거 야무지네요...


val rotateCnt = readln().toInt()

val rotateInfoList = Array(rotateCnt) {
val (gearNum, dir) = readln().split(" ").map { it.toInt() }
RotateInfo(gearNum, dir)
}

rotateInfoList.forEach { rotateInfo ->

val (gearNum, dir) = rotateInfo

val (firstLeft, firstRight) = gear1.getPole()
val (secondLeft, secondRight) = gear2.getPole()
val (thirdLeft, thirdRight) = gear3.getPole()
val (fourthLeft, fourthRight) = gear4.getPole()

// 회전에 영향을 받을 기어 판정
if (firstRight != secondLeft) {
affectedInfo[0].add(1)
affectedInfo[1].add(0)
}
if (secondRight != thirdLeft) {
affectedInfo[1].add(2)
affectedInfo[2].add(1)
}
if (thirdRight != fourthLeft) {
affectedInfo[2].add(3)
affectedInfo[3].add(2)
}
Comment on lines +78 to +121
Copy link
Member

Choose a reason for hiding this comment

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

val gearList = Array(4) {
    Gear(readln())
}.toList()

val affectedInfo = Array(4) {
    mutableListOf<Int>()
}

fun solution() {

    val rotateCnt = readln().toInt()

    val rotateInfoList = Array(rotateCnt) {
        val (gearNum, dir) = readln().split(" ").map { it.toInt() }
        RotateInfo(gearNum, dir)
    }

    rotateInfoList.forEach { rotateInfo ->

        val (gearNum, dir) = rotateInfo

        gearList.windowed(2, 1).forEachIndexed { index, gears ->
            val (prev, next) = gears.map { it.getPole() }
            if (prev.second != next.first) {
                affectedInfo[index].add(index + 1)
                affectedInfo[index + 1].add(index)
            }
        }

어... 이런거 있을거같은데? 찾아보면 있더라구요!
머리를 쓰지 않으면 고생합니다 ㅠ

저도 windowed 같은게 있는지 몰랐어요


// 지정된 기어 회전
rotateGear(gearNum - 1, dir)

affectedInfo.clear()
Copy link
Member

Choose a reason for hiding this comment

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

Array.clear() 만드실거면 그냥 mutableList를 쓰시죠

}

var score = 0
repeat(4) { cnt ->
score += gearList[cnt].getTopState() * twoToPowOfExponent(cnt)
}
Comment on lines +129 to +132
Copy link
Member

Choose a reason for hiding this comment

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

이건 이제 안할때 된거같지만

val score = gearList.mapIndexed { index, gear ->
    gear.getTopState() * twoToPowOfExponent(index)
}.sum()

택 1

val score = (0 until 4).sumOf { gearList[it].getTopState() * twoToPowOfExponent(it) }


println(score)

}

fun rotateGear(gearNum: Int, dir: Int) {
val targetGear = gearList[gearNum]
targetGear.rotate(dir)
val changedDir = dir.dirChange()
val relatedGearList = mutableListOf<Int>()

affectedInfo[gearNum].forEach { relatedGearNum ->
relatedGearList.add(relatedGearNum)
}
Comment on lines +142 to +146
Copy link
Member

Choose a reason for hiding this comment

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

val relatedGearList = affectedInfo[gearNum].toList()

랑 뭐가 달라요?


relatedGearList.forEach { relatedGearNum ->
affectedInfo[gearNum].remove(relatedGearNum)
affectedInfo[relatedGearNum].remove(gearNum)
}

relatedGearList.forEach {
rotateGear(it, changedDir)
}
}

// 2차원 배열 안의 리스트들을 초기화
fun <T> Array<MutableList<T>>.clear() {
this.forEach {
it.clear()
}
}
Comment on lines +159 to +163
Copy link
Member

Choose a reason for hiding this comment

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

한번 사용하는 함수를 만들 필요가 있을까 싶어요


// 2의 거듭제곱
fun twoToPowOfExponent(exponent: Int): Int {
var powered = 1
if (exponent == 0) return 1
repeat(exponent) {
powered *= 2
}
return powered
}

// 방향 바꿔주기
fun Int.dirChange() = if (this == -1) 1 else -1

fun <T> Array<T>.second(): T = this[1]
fun <T> Array<T>.third(): T = this[2]
fun <T> Array<T>.fourth(): T = this[3]
Comment on lines +178 to +180
Copy link
Member

Choose a reason for hiding this comment

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

@soopeach ???...

Copy link
Member Author

Choose a reason for hiding this comment

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

@gongdongho12
보기 좋으라고 해봤는데...ㅎㅎ 안쓰느니만 못하나요??

Copy link
Member

Choose a reason for hiding this comment

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

이거 왜 안지우는거에요? Iterator는 그냥 자동으로 destructure 지원되서 사용 안해도 되요!

}

fun main(){
val myClass = 전현수_톱니바퀴()
myClass.solution()
}


60 changes: 60 additions & 0 deletions src/4week/hyunsoo/트리의 부모 찾기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package `4week`.hyunsoo

import java.util.*

/**
* <문제>
* [트리의 부모 찾기](https://www.acmicpc.net/problem/11725)
*
* 루트없는 트리가 주어짐.
* 트리의 루트는 1. 각 노드의 부모를 구하라.
*/

class 전현수_트리의_부모_찾기{

val rootQueue: Queue<Int> = ArrayDeque()
Copy link
Member

Choose a reason for hiding this comment

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

ArrayDeque보다는 LinkedList가 Queue 자료구조에 더 효율적이에요!

val nodeCnt = readln().toInt()
val treeArray = Array(nodeCnt + 1) { mutableListOf<Int>() }
Copy link
Member

Choose a reason for hiding this comment

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

중첩되지 않고 사실 순서보다는 실제 Collection의 값을 삭제하는 데에 초점을 맞췄게 때문에
mutableList 보다는 mutableSet 사용하시는게 이 로직에서는 훨씬 효율적이에요!

val parentNodeData = IntArray(nodeCnt + 1)

// 루트는 1
val initParent = 1

fun solution() {


repeat(nodeCnt - 1) {
val (a, b) = readln().split(" ").map { it.toInt() }
treeArray[a].add(b)
treeArray[b].add(a)
}
findParentNode()
parentNodeData.drop(2).forEach {
println(it)
}
Comment on lines +32 to +34
Copy link
Member

Choose a reason for hiding this comment

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

(minor) 중요하진 않지만 성능개선은 큰폭이에요! 나름 반복인지라

println(parentNodeData.drop(2).joinToString("\n"))


}

fun findParentNode() {

rootQueue.add(initParent)

while (rootQueue.isNotEmpty()) {
val curParent = rootQueue.poll()
// 부모와 연결되어있는 것들 자식으로 판정
treeArray[curParent].forEach { child ->
parentNodeData[child] = curParent
// 방문 판정 -> 자식 노드에서 부모 노드 제거
treeArray[child].remove(curParent)
Copy link
Member

Choose a reason for hiding this comment

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

MutableSet이면 더 효율적으로 삭제될 수 있는 이유

rootQueue.add(child)
}
}

}

}

fun main(){
val myClass = 전현수_트리의_부모_찾기()
myClass.solution()
}
52 changes: 52 additions & 0 deletions src/4week/hyunsoo/회의실 배정.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package `4week`.hyunsoo

/**
* <문제>
* [회의실 배정](https://www.acmicpc.net/problem/1931)
*
* 한 개의 회의실이 있는데 이를 사용하고자 하는 N개의 회으ㅔㅇ 대하여 회의실 사용표를 만드려고 함.
* 각 회의 I에 대해 시작시간과 끝나는 시간이 주어져있고, 각 회의가 겹치지 않게 하면서 회의실을 사용할 수 있는 회의의 최대 개수 찾기
*
* 아이디어
* - 회의의 수가 100,000개라 완탐은 불가능
*/

class 전현수_회의실_배정{

data class MeetingData(val start: Int, val end: Int)

fun solution() {

val meetingCnt = readln().toInt()
val meetingList = mutableListOf<MeetingData>()
var canMeetingCnt = 0
var endTime = 0
Copy link
Member

@gongdongho12 gongdongho12 Nov 3, 2022

Choose a reason for hiding this comment

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

항상 전역변수를 선언해줘야한다는 부분을 사용하는 부분에만 적용한다 생각해보시면 아래 endTime을 사용하는 부분에서 var 선언해주면 되지 않았을까 싶어요!


repeat(meetingCnt) {
val (start, end) = readln().split(" ").map { it.toInt() }
meetingList.add(MeetingData(start, end))
}

// 정렬
meetingList.sortWith(compareBy<MeetingData> { it.end }.thenBy { it.start })
Comment on lines +21 to +31
Copy link
Member

Choose a reason for hiding this comment

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

소팅을 통해 추가해야 하는 경우에는 PriorityQueue를 이용해봐요
이럴때 쓰라고 만들어진 친구입니다!

val pq = PriorityQueue(compareBy<MeetingData> { it.end }.thenBy { it.start })


// 정렬된 첫 미팅은 반드시 해야지!
endTime = meetingList.first().end
canMeetingCnt++

meetingList.drop(1).forEach { meetingData ->
if (endTime <= meetingData.start) {
endTime = meetingData.end
canMeetingCnt++
}
}
Comment on lines +34 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.

pq를 사용하면 drop 처리 안하고 그냥 뽑아주면 되겠죠?

endTime = pq.poll().end
canMeetingCnt++

while (pq.isNotEmpty()) {
    val meetingData = pq.poll()
    if (endTime <= meetingData.start) {
        endTime = meetingData.end
        canMeetingCnt++
    }
}


println(canMeetingCnt)
}

}

fun main(){
val myClass = 전현수_회의실_배정()
myClass.solution()
}