๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Problem Solving

[Java] ๋ฐฑ์ค€ 7579 - ์•ฑ

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();
	}
}