본문 바로가기
자바 코딩테스트

백준 29723 브실이의 입시전략

by 백엔드 개발자 2025. 4. 17.

특정 과목들의 성적의 합으로 서류전형 합격여부를 결정한다.

요구하는 과목수와 반영된다고 공개된 과목이 주어질 경우

최소 점수와 최대 점수 구하기.

과목은 중복되지 않는다.

 

수강 과목수N  요구 과목수 M 대학에서 공개한 과목수 K

 

6 3 2
calculus 100
probability 70
physics 50
chemistry 80
python 90
algorithm 100
physics
python

 

요컨데 대학에서 공개한 과목수는 정해진 상태에서

M-K 과목수를 N-K 수강과목수 중에서 고르는 경우를 생각하면 될 것 같다.

 

Map을 사용해보면 좋을 것 같다.

K로 받은 값들을 Map에서 꺼내서 더해 놓고

값들 중 최대 최소를 구하기

점수들을 정렬해놓고 M-K개 만큼 앞뒤에서 더해주면 될 것 같다.

 

 

 

 

풀이 방식

 

1.  split으로 떼내서 값들을 세팅한다.

2. Map을 사용한 다음 먼저 선택된 과목들은 미리 더해놓고 Map 리스트에서 제외한다.

4. Map의 값들을 오름차순으로 정렬하고 최소,최대를 각각 맨앞과 맨뒤에서 추가로 요구되어야 할 과목수 ( M-K개)만큼 더해서 구하는 방식으로 풀었다.

package 백준.브실이의입시전략29723;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Map<String, Integer> map = new HashMap<>();
        int result = 0;
        int minResult = 0;
        int maxResult = 0;
        String count = br.readLine();
        int[] list = Arrays.stream(count.split(" ")).mapToInt(Integer::parseInt).toArray();
        int totalIndex = list[0];
        int needIndex = list[1];
        int providedIndex = list[2];

        for (int i = 0; i < totalIndex; i++) {
            String[] str = br.readLine().split(" ");
            map.put(str[0], Integer.parseInt(str[1]));
        }
        for (int i = 0; i < providedIndex; i++) {
            String choice = br.readLine();
            result += map.get(choice);
            map.remove(choice);
        }

        // map의 값들을 오름차순 정렬
        int [] lists = map.values().stream().mapToInt(i -> i).toArray();
        Arrays.sort(lists);
        for (int i = 0; i < needIndex-providedIndex; i++) {
            minResult += lists[i];
            maxResult += lists[(totalIndex-1)-i-providedIndex];
        }
        minResult += result;
        maxResult += result;

        System.out.printf("%d %d\n", minResult, maxResult);
        br.close();
    }
}