https://www.acmicpc.net/problem/17952
접근 :
- 과제가 들어온 경우 즉시 완료 되는지 확인한다.
- 즉시 완료가 되지 않는 경우, 현재 진행중인 과제에 추가한 후 1회 수행한다.(이전에 진행중인 과제가 있는 경우 stack에 보관)
- 과제가 들어오지 않은 경우, 현재 진행중인 과제가 있으면 진행 후 완료 여부를 확인한다.
- 과제가 완료 될 때마다 점수를 더한 후 출력한다.
코드구현 :
import java.io.*;
import java.util.*;
public class Main {
static class Subject{
private int score;
private int time;
public Subject(int score, int time){
this.score = score;
this.time = time;
}
public int doSubject(){
return --this.time;
}
public int getScore(){
return this.score;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Stack<Subject> subjects = new Stack<>();
Subject now = null;
int answer = 0;
for(int i = 0 ; i < n ; i++){
String[] temp = br.readLine().split(" ");
//과제가 들어오지 않은 경우
if(temp.length <= 1){
if(now == null) continue;
// 진행중인 과제를 1번 수행하고 완료되었는지 확인
if(now.doSubject() == 0){
answer += now.getScore();
now = subjects.isEmpty() ? null : subjects.pop();
}
continue;
}
//과제가 들어온 경우
int score = Integer.parseInt(temp[1]);
int time = Integer.parseInt(temp[2]);
// 즉시 완료 가능한지 확인
if(time == 1) {
answer += score;
continue;
}
// 진행중이던 과제가 있는 경우 stack에 보관
if(now != null) subjects.push(now);
now = new Subject(score, time);
now.doSubject();
}
System.out.println(answer);
}
}
'JAVA > Coding Test' 카테고리의 다른 글
[JAVA] 백준 2470 두 용액_투포인터 (1) | 2024.06.03 |
---|---|
[JAVA] 백준 7795 먹을 것인가 먹힐 것인가_정렬 (0) | 2024.06.03 |
[JAVA] 프로그래머스 연습 징검다리 (0) | 2024.05.30 |
[JAVA] 백준 19583 싸이버 개강총회 (0) | 2024.05.29 |
[JAVA] 프로그래머스 연습 요격 시스템 (0) | 2024.05.28 |