https://www.acmicpc.net/problem/7579
ํ์ด
์ผํ ๋ด๋ ๋ฐฐ๋ญ ๋ฌธ์ ๋ฅผ ๋ง์ด ๋ฎ์ ์๋ค
dp[i][w] = ์ต๋ ๋ฌด๊ฒ๊ฐ w์ธ ๋ฐฐ๋ญ์์ i๋ฒ์งธ ๋ฌผ๊ฑด๊น์ง ํ๋จํ์ ๋์ ์ต๋ ๊ฐ์น
dp[i][m] = ์ต๋ ๋ฉ๋ชจ๋ฆฌ๊ฐ m์ธ ์ํ์์ i๋ฒ์งธ ์ฑ๊น์ง ํ๋จํ์ ๋์ ์ต๋ ๋น์ฉ
ํ์ง๋ง ์ด๋ ๊ฒ ํ๊ธฐ์๋ m์ด ์ต๋ 10,000,000๊น์ง ์ปค์ ธ์ ์๋จ
๋์ ๊ธฐ์ค์ ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์๋ ๋น์ฉ์ผ๋ก ์ก์ผ๋ฉด
dp[i][c] = ์ต๋ ๋น์ฉ์ด c์ธ ์ํ์์ i๋ฒ์งธ ์ฑ๊น์ง ํ๋จํ์ ๋์ ์ต๋ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋
์ฌ๊ธฐ์ c๋ ์ต๋ 100×100
์์ ์์ ์ด์ฉํด์ ์ฐ๋ฆฌ ์ํ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ด๊ณผํ๋ ๋น์ฉ์ ์ต์๋ฅผ ๊ตฌํ๋ฉด ๋!
์ฝ๋
๋๋ณด๊ธฐ
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int appsCnt = Integer.parseInt(st.nextToken());
int targetMemory = Integer.parseInt(st.nextToken());
int[] memories = new int[appsCnt + 1];
int[] costs = new int[appsCnt + 1];
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= appsCnt; i++) {
memories[i] = Integer.parseInt(st.nextToken());
}
int costSum = 0;
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= appsCnt; i++) {
costs[i] = Integer.parseInt(st.nextToken());
costSum += costs[i];
}
int[][] dp = new int[appsCnt + 1][costSum + 1];
int minCost = costSum;
for (int i = 1; i <= appsCnt; i++) {
for (int c = 0; c <= costSum; c++) {
if (c - costs[i] >= 0) {
dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - costs[i]] + memories[i]);
} else {
dp[i][c] = dp[i - 1][c];
}
if (dp[i][c] >= targetMemory) {
minCost = Math.min(minCost, c);
}
}
}
System.out.println(minCost);
br.close();
}
}
'Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ฐฑ์ค 1707 - ์ด๋ถ ๊ทธ๋ํ (0) | 2024.04.09 |
---|---|
[Java] ๋ฐฑ์ค 16724 - ํผ๋ฆฌ ๋ถ๋ ์ฌ๋์ด (0) | 2024.03.28 |
[Java] ๋ฐฑ์ค 21276 - ๊ณ๋ณด ๋ณต์๊ฐ ํธ์ (0) | 2024.03.26 |
[Java] ๋ฐฑ์ค 31577 - ๋์ฌ์จ์ด์ ๋นํธ์ฝ์ธ (0) | 2024.03.22 |
[Java] ๋ฐฑ์ค 31623 - Room Temperature (0) | 2024.03.21 |