https://www.acmicpc.net/problem/13302
13302번: 리조트
수영이는 여름방학을 맞이하여 많은 놀이 시설이 있는 KOI 리조트에 놀러가려고 한다. 리조트의 하루 이용권의 가격은 만원이다. 하지만 리조트의 규모는 상상을 초월하여 모든 시설을 충분히
www.acmicpc.net
저는 DP를 이용해 문제를 해결하였습니다.
각각의 일에 리조트를 가지 못하는 일을 제외하고 1 3 5일 이용권을 사용해보고 각각의 경우에 대해 가장 작은 값을 구하였습니다. 이때 중복되는 계산이 존재하기에 이를 DP를 이용해 중복된 계산을 하지 않도록 하였습니다.
package BOJ.dp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_13302 {
private static final int[] MONEY = new int[] {10_000, 25_000, 37_000};
private static final int[] DAY = new int[] {1, 3, 5};
private static final int[] COUPON = new int[] {0, 1, 2};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int m = stoi.apply(st.nextToken());
boolean[] noReservation = new boolean[n+1];
if(m != 0){
st = new StringTokenizer(br.readLine());
for(int i = 0 ; i < m ; i++){
int day = stoi.apply(st.nextToken())-1;
noReservation[day] = true;
}
}
int[][] dp = new int[n+1][2*n+1];
for(int i = 0 ; i <= n ; i++){
Arrays.fill(dp[i],NOT_VALID);
}
int result = cal(0,0,noReservation,n,dp);
System.out.println(result);
}
private static final int INF = 987654321;
private static final int NOT_VALID = -1;
private static int cal(int depth, int coupon, boolean[] noReservation, int n,int[][] dp) {
if(depth >= n){
return 0;
}
if(noReservation[depth]){
return cal(depth+1,coupon,noReservation,n,dp);
}
if(dp[depth][coupon] != NOT_VALID){
return dp[depth][coupon];
}
int result = INF;
if(coupon >= 3){
result = Math.min(result,cal(depth+1,coupon - 3,noReservation,n,dp));
}
for(int i = 0 ; i < 3; i++){
result = Math.min(result,cal(depth+DAY[i],coupon+COUPON[i],noReservation,n,dp) + MONEY[i]);
}
dp[depth][coupon] = result;
return result;
}
}
'알고리즘' 카테고리의 다른 글
백준 2615번 오목 (JAVA) (0) | 2023.10.27 |
---|---|
백준 1487번 물건 팔기 (JAVA) (0) | 2023.10.26 |
백준 15624번 피보나치 수7 (JAVA) (1) | 2023.10.24 |
백준 1951번 활자 (JAVA) (1) | 2023.10.23 |
프로그래머스 수열과 구간 쿼리2(JAVA) (0) | 2023.10.22 |