백준 31869번 선배님 밥 사주세요! Java

: ) YOUNG·2024년 5월 30일
1

영어 공부

목록 보기
43/59
post-thumbnail

백준 31869번
https://www.acmicpc.net/problem/31869

문제



생각하기


  • 자료구조 문제입니다.

  • 주와 요일 크기로 약속정보 타입으로 2차원배열을 만들어서 관리합니다.



동작

이 문제의 핵심이라면..

    private static HashMap<String, Integer> seniorList;
    private static ArrayList<Appointment>[][] arr;

약속정보를 자료구조로 관리하는 거? 딱히 그외에는 특별한 점은 없는듯



결과


코드



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

public class Main {

    // input
    private static BufferedReader br;

    // variables
    private static int N;
    private static HashMap<String, Integer> seniorList;
    private static ArrayList<Appointment>[][] arr;

    private static class Appointment {
        String name;
        int value;

        private Appointment(String name, int value) {
            this.name = name;
            this.value = value;
        }
    } // End of Appointment class

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

        input();

        bw.write(solve());
        bw.close();
    } // End of main()

    private static String solve() {
        StringBuilder sb = new StringBuilder();

        int ans = 0;
        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 7; j++) {
                boolean flag = false;
                List<Appointment> promises = arr[i][j];

                for (Appointment promise : promises) {
                    if (seniorList.get(promise.name) >= promise.value) {
                        seniorList.put(promise.name, seniorList.getOrDefault(promise.name, 0) - promise.value);
                        flag = true;
                        break;
                    }
                }

                if (flag) {
                    count++;
                } else {
                    ans = Math.max(ans, count);
                    count = 0;
                }
            }
        }
        sb.append(Math.max(ans, count));
        return sb.toString();
    } // End of solve()

    private static void input() throws IOException {
        N = Integer.parseInt(br.readLine());
        seniorList = new HashMap<>();
        StringTokenizer st;
        arr = new ArrayList[11][7];

        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 7; j++) {
                arr[i][j] = new ArrayList<>();
            }
        }

        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            String s = st.nextToken();
            int w = Integer.parseInt(st.nextToken());
            int d = Integer.parseInt(st.nextToken());
            int p = Integer.parseInt(st.nextToken());

            arr[w][d].add(new Appointment(s, p));
        }

        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            String name = st.nextToken();
            int value = Integer.parseInt(st.nextToken());
            seniorList.put(name, value);
        }

    } // End of input()
} // End of Main class

0개의 댓글