PS/Problems
[Java] 백준 7579번 앱 - 배낭 문제 응용
CalicoCat22
2022. 2. 9. 21:27
https://www.acmicpc.net/problem/7579
7579번: 앱
입력은 3줄로 이루어져 있다. 첫 줄에는 정수 N과 M이 공백문자로 구분되어 주어지며, 둘째 줄과 셋째 줄에는 각각 N개의 정수가 공백문자로 구분되어 주어진다. 둘째 줄의 N개의 정수는 현재 활
www.acmicpc.net
배낭 문제의 응용편이다.
기존 문제는
(무게 제한 안넘기기 -> 가치 최대화) 였다면
(메모리 제한 넘기기 -> 코스트 최소화) 여서 발상이 쉽지 않았다.
import java.util.*;
import java.io.*;
public class g3_7579 {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static int n, m;
static List<App> apps = new ArrayList<>();
static int[][] pp;
public static void main(String[] args) {
n = fs.nextInt();
m = fs.nextInt();
for (int i=0;i<n;i++){
apps.add(new App(fs.nextInt(), 0));
}
for (int i=0;i<n;i++){
apps.get(i).cost = fs.nextInt();
}
int ans = Integer.MAX_VALUE;
pp = new int[10002][n + 1];
for (int col=1;col<=n;col++){
App now = apps.get(col - 1);
for (int row=1;row<=10001;row++){
if (now.cost > row - 1){
pp[row][col] = pp[row][col - 1];
}
else{
pp[row][col] = Math.max(pp[row][col - 1], pp[row - now.cost][col - 1] + now.mem);
}
if (pp[row][col] >= m){
ans = Math.min(ans, row - 1);
}
}
}
pw.println(ans);
pw.close();
}
static class App {
int mem, cost;
public App(int mem, int cost){
this.mem = mem;
this.cost = cost;
}
}
// ----------input function----------
static void sort(int[] a) {
ArrayList<Integer> L = new ArrayList<>();
for (int i : a)
L.add(i);
Collections.sort(L);
for (int i = 0; i < a.length; i++)
a[i] = L.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}