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

[BOJ] 13164 #24

Open
HyomK opened this issue Jan 28, 2023 · 0 comments
Open

[BOJ] 13164 #24

HyomK opened this issue Jan 28, 2023 · 0 comments

Comments

@HyomK
Copy link
Owner

HyomK commented Jan 28, 2023

백준 13154 행복 유치원
[문제유형] 그리디
[해결방법] 정렬
image

  val group  = readLine().split(" ").map{it.toLong()}.toMutableList()
        val gap = LongArray(N-1){0}
        for(i in 0 until group.size-1){
            gap[i]=(group[i+1]-group[i])
        }
        gap.sort()
        var result = 0L

        for(j in 0 until N-Kl){
            result+=gap[j]
        }
        println(result)
  1. 정렬된 키순서에서 앞뒤 차이를 구한다 (gap)
  2. N명을 K개의 묶음으로 만든다는 것은 N-K 개의 연속된 값이 나타난다는 것
    image
    ex) 5개를 3묶음으로 만든다
    그림에서도 연속된 값이 5-3 =2 개가 발생
  3. 앞에서 구한 차를 오름차순 정렬하여 앞에서 N-K개 선택하여 합한 값이 최소 비용이다
HyomK added a commit that referenced this issue Jan 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant