본문 바로가기
Problem Solving/Baekjoon

[백준] 10774 저지 - Greedy / Java

by graycode 2024. 1. 11.

 문제 링크

 

10774번: 저지

학교 대표팀은 1부터 번호가 매겨진 저지를 학생 선수들에게 배분하고자 한다. 저지의 사이즈는 S, M, L 중 하나이다 (물론 S=small, M=medium, L=Large다). 각각의 선수들은 구체적인 저지의 번호와 선호

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read(), m = read();

        int[] arr = new int[n + 1];
        for (int i = 1; i <= n; i++) arr[i] = convert(read());

        int cnt = 0;
        while (m-- > 0) {
            int c = convert(read()), i = read();

            if (arr[i] != 0 && arr[i] <= c) {
                arr[i] = 0;
                cnt++;
            }
        }

        bw.write(String.valueOf(cnt));
        bw.flush();
    }

    private static int convert(int i) {
        return i == 12 ? 1 : (i == 13 ? 2 : 3);
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글