[백준] 10800 - 컬러볼 (JAVA)

우태·2023년 6월 22일
0

Algorithm

목록 보기
3/4

문제

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


풀이

크기와 색상 정보가 포함된 공이 주어지고 각각의 공과 색이 다르면서 크기가 작은 공들의 크기의 합을 구하는 문제
\to 크기 기준 오름차순으로 정렬하고 크기의 총합과 색상별 크기의 총합을 구한 뒤 정렬된 배열을 순회하면서 해당 공까지의 총합에서 해당 공 색상의 총합을 뺀 값을 출력

  1. 공 객체의 배열을 크기 기준 오름차순으로 정렬
  2. 순회하면서 해당 공의 크기를 총합과 색상별 총합에 저장
  3. 해당 공까지의 총합에 색상별 총합 값을 빼고 입력 순서를 유지하며 저장

소스 코드

import java.io.*;
import java.util.*;

public class Main {
    public static class Player implements Comparable<Player>{
        int idx, color, size;

        public Player(int idx, int color, int size) {
            this.idx = idx;
            this.color = color;
            this.size = size;
        }

        @Override
        public int compareTo(Player o) {
            return this.size - o.size;
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        Player[] players = new Player[n];

        for(int i=0; i<n; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());

            int color = Integer.parseInt(st.nextToken());
            int size = Integer.parseInt(st.nextToken());

            players[i] = new Player(i, color, size);
        }

        Arrays.sort(players);

        int[] colors = new int[n];
        int[] scores = new int[n];
        int idx = 0;
        int total = 0;

        for(int i=0; i<n; i++){
            Player current = players[i];

            while(players[idx].size < current.size) {
                total += players[idx].size;
                colors[players[idx].color-1] += players[idx].size;
                idx++;
            }
            scores[current.idx] = total - colors[current.color-1];
        }

        for(int i : scores){
            System.out.println(i);
        }
    }
}

0개의 댓글