Coding/KOI

[Baekjoon, KOI] 20186 - Choosing Numbers

xeohyuni 2023. 4. 16. 20:12

Problem

N natural numbers are arranged from side to side. You have to choose K from these. When you choose, you have to choose all K at once.

You will calculate the number of points for each number you choose. The score is the number of your own minus the number of selected numbers on your left. After calculating the score for each of these even numbers, the total score is the sum of the calculated scores. You have to choose K numbers to maximize your overall score.

For example, N = 5 natural numbers are given in order 2 3 1 2 1 and let's say K = 3. If the first 2 and the two 1s are selected, the calculation of the scores of each number is equal to 2 0 -1. Therefore, the overall score is 1. If you choose the first 2 and 3 and the second 2 and list the scores of each number, it is equal to 2 2 0. So the overall score is 4. In this example, the maximum value of the total score is 4.

Given an array of N natural numbers and a positive integer K, write a program that outputs the maximum value of the total score.

Restrictions

  • 1 ≤ N ≤ 5 000
  • 1 ≤ K ≤ N
  • The number of natural numbers given is more than 1 and less than 100000
Example Question 1 (Insert) Example Question 2 (Result)
5 3
2 3 1 2 1
4
Example Question 2 (Insert) Example Question 2 (Result)
6 2
4 1 5 2 6 3
 

Solution

The key point to solve this problem is to find the max numbers to create the largest result. We need to sort the numbers to choose the top biggest numbers depending the K. It is also significant to change the list elements into integer for calculation!

N,K = map(int,input().split())#N개의 자연수랑 K개 고르기
arr = list(map(int,input().split())) #리스트안에 요소 int로 바꾸기
arr.sort(reverse = True)#리스트 정렬 순서대로
result = 0 #숫자 초기화
for i in range(K):#K안에 숫자들중에 가장 큰수 고를때
    result += arr[i]-i# 뒤에서 가장 큰 숫자만 뽑기
print(result)# 출력

Discussion

I forgot how to append the elements and return into result. It was to create a spare list and a varible to append my calculated datas/elements in there!

 

 

 

 

'Coding > KOI' 카테고리의 다른 글

백준 KOI 2596번 - 편지 letter  (2) 2023.10.08