알고리즘/자료구조

[BOJ] 백준 17952 - 과제는 끝나지 않아 풀이

송승현(SSH) 2022. 10. 20. 00:57

1. 문제

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

 

17952번: 과제는 끝나지 않아!

성애는 이번 학기에 전공을 정말 많이 듣는다. 이로 인해 거의 매일을 과제를 하면서 보내고 있다. 그런데도 과제가 줄어들 기미가 보이지 않는데, 바로 분단위로 과제가 추가되고 있기 때문이

www.acmicpc.net

 

2. 풀이

백준 실버 3의 문제이다.
첫째 줄의 입력을 제외하고 입력을 받을 때 1이 있다면 과제를 저장하는 리스트에 과제를 넣고 아니라면 1분씩 단축하는 함수를 실행한다.
함수를 실행하는 이유는 첫째 줄의 입력을 제외하고 입력을 받을 때마다 1분씩 지나기 때문에 N번을 수행하면서 한 번 수행할 때마다 받은 과제의 남은 시간을 1분씩 단축시킨다.

단축시켰을 때 남은 과제의 시간이 0이라면 다 한것이므로 점수에 더한다.

3. 소스 코드

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    static class Subject {
        int score;
        int min;

        public Subject(int score, int min) {
            this.score = score;
            this.min = min;
        }
    }
    static List<Subject> subjectList = new ArrayList<>();
    static int result = 0;

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

        int cnt = Integer.parseInt(br.readLine());
        for (int i = 0; i < cnt; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            if (st.nextToken().equals("1")) {
                Subject subject = new Subject(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                subjectList.add(subject);
            }
            subjectWork();
        }

        bw.write(Integer.toString(result));
        bw.flush();
        bw.close();
        br.close();
    }

    static public void subjectWork() {
        if (!subjectList.isEmpty()) {
            if (subjectList.get(subjectList.size() - 1).min != 0) {
                subjectList.get(subjectList.size() - 1).min -= 1;
            }

            if (subjectList.get(subjectList.size() - 1).min == 0) {
                result += subjectList.get(subjectList.size() - 1).score;
                subjectList.remove(subjectList.get(subjectList.size() - 1));
            }
        }
    }
}