알고리즘

백준 29160번 나의 FIFA 팀 가치는? (JAVA)

박카스마시며코딩 2023. 9. 4. 12:46

https://www.acmicpc.net/problem/29160

 

29160번: 나의 FIFA 팀 가치는?

첫 번째 줄에 선수의 수 $N$과 $K$가 공백으로 구분되어 주어진다. $(0\leq N\leq 1\,000\,000;$ $1\leq K\leq 50\,000)$ 두 번째 줄부터 $N$개의 줄에 걸쳐 각 줄에 $i$번째 선수의 포지션 $P_{i}$, 선수 가치 $W_{i}$가

www.acmicpc.net

 

저는 우선순위 큐를 통해 문제를 해결였습니다.

먼저 각 포지션마다 우선순위 큐를 두어서 각 포지션 별로 가장 가치가 높은 사람부터 나오도록 하였습니다.

3월은 각 포지션을 돌면서 가장 가치가 큰 사람을 팀으로 넣고, 8월은 팀에 있는 모든 사람의 가치를 1씩 줄이고 11월은 다시 가장 가치가 큰 사람을 팀에 넣어 문제를 해결하였습니다.

 

package BOJ.greedy;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_29160 {
    private static final int SIZE = 11;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Function<String,Integer> stoi = Integer::parseInt;
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = stoi.apply(st.nextToken());
        int k = stoi.apply(st.nextToken());
        PriorityQueue<Integer>[] pq = new PriorityQueue[SIZE+1];
        for(int i = 0 ; i <= SIZE ; i++){
            pq[i] = new PriorityQueue<>(Collections.reverseOrder());
        }
        for(int i = 0 ; i < n ; i++){
            st = new StringTokenizer(br.readLine());
            int position = stoi.apply(st.nextToken());
            int value = stoi.apply(st.nextToken());
            pq[position].add(value);
        }
        int teamValue = calAfterKYearTeamValue(pq,k);
        System.out.println(teamValue);
    }

    private static int calAfterKYearTeamValue(PriorityQueue<Integer>[] pq, int k) {
        int[] teamValue = new int[SIZE+1];
        for(int i = 0 ; i < k ; i++){
            for(int j = 1 ; j <= SIZE ; j++){ // 3월
                if(!pq[j].isEmpty() && teamValue[j] < pq[j].peek()){
                    pq[j].add(teamValue[j]);
                    teamValue[j] = pq[j].poll();
                }
            }
            for(int j = 1 ; j <= SIZE ; j++){ // 8월
                teamValue[j] = Math.max(teamValue[j]-1,0);
            }
            for(int j = 1 ; j <= SIZE ; j++){ // 11월
                if(!pq[j].isEmpty() && teamValue[j] < pq[j].peek()){
                    pq[j].add(teamValue[j]);
                    teamValue[j] = pq[j].poll();
                }
            }
        }
        int sumTeamValue = 0;
        for(int j = 1 ; j <= SIZE ; j++){
            sumTeamValue += teamValue[j];
        }
        return sumTeamValue;
    }
}