[SWEA SW Expert Academy] 1952. [모의 SW 역량테스트] 수영장 (JAVA)
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PpFQaAQMDFAUq
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
김 프로는 수영장을 이용한다.
김 프로는 지출이 너무 많아 내년 1년 동안 각 달의 이용 계획을 수립하고 가장 적은 비용으로 수영장을 이용할 수 있는 방법을 찾고 있다.
수영장에서 판매하고 있는 이용권은 아래와 같이 4 종류이다.
① 1일 이용권 : 1일 이용이 가능하다.
② 1달 이용권 : 1달 동안 이용이 가능하다. 1달 이용권은 매달 1일부터 시작한다.
③ 3달 이용권 : 연속된 3달 동안 이용이 가능하다. 3달 이용권은 매달 1일부터 시작한다.
(11월, 12월에도 3달 이용권을 사용할 수 있다 / 다음 해의 이용권만을 구매할 수 있기 때문에 3달 이용권은 11월, 12월, 1윌 이나 12월, 1월, 2월 동안 사용하도록 구매할 수는 없다.)
④ 1년 이용권 : 1년 동안 이용이 가능하다. 1년 이용권은 매년 1월 1일부터 시작한다.
각 달의 이용 계획은 [Table 1]의 형태로 수립된다.
이용 계획에 나타나는 숫자는 해당 달에 수영장을 이용할 날의 수를 의미한다.
각 이용권의 요금과 각 달의 이용 계획이 입력으로 주어질 때,
가장 적은 비용으로 수영장을 이용할 수 있는 방법을 찾고 그 비용을 정답으로 출력하는 프로그램을 작성하라.
이 문제를 해결하기 위해서 DP(다이나믹 프로그래밍)으로 문제를 해결했습니다.
각 각의 월이 지나갈 때 마다 가장 최소가 되는 값을 찾아서 그 달에 가격에 넣어주는 방식입니다.
한달이 지났을때 어떤 표를 사야 저렴한지 비교를 진행하게 됩니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int T = Integer.parseInt(st.nextToken());
for (int t = 1; t <= T; t++) {
int price[] = new int[4];
int month[] = new int[13];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < 4; i++) {
price[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine(), " ");
for (int i = 1; i < month.length; i++) {
month[i] = Integer.parseInt(st.nextToken());
}
int monthlyPrice[] = new int[13];
for (int i = 1; i < monthlyPrice.length; i++) {
int dayPrice = price[0] * month[i] + monthlyPrice[i - 1]; //하루권
int monthPrice = price[1] + monthlyPrice[i - 1]; //한달권
int threeMonthPrice = Integer.MAX_VALUE; //3개월권
int yearPrice = Integer.MAX_VALUE; //1년권
if (i >= 3) { //3월부터는 3개월권 구매가능
threeMonthPrice = price[2] + monthlyPrice[i - 3];
}
if (i == 12) { //1년권을 살 수 있음
yearPrice = price[3];
}
monthlyPrice[i] = Math.min(dayPrice, Math.min(monthPrice, Math.min(threeMonthPrice, yearPrice)));
}
System.out.println("#" + t + " " + monthlyPrice[12]);
}
}
}