Hello Ocean! ๐ŸŒผ

[๋ฐฑ์ค€/C++] 12865. ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ ๋ณธ๋ฌธ

Algorithm

[๋ฐฑ์ค€/C++] 12865. ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ

bba_dda 2021. 3. 14. 23:33
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

www.acmicpc.net/problem/12865

 

12865๋ฒˆ: ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ

์ฒซ ์ค„์— ๋ฌผํ’ˆ์˜ ์ˆ˜ N(1 ≤ N ≤ 100)๊ณผ ์ค€์„œ๊ฐ€ ๋ฒ„ํ‹ธ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ K(1 ≤ K ≤ 100,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑฐ์ณ ๊ฐ ๋ฌผ๊ฑด์˜ ๋ฌด๊ฒŒ W(1 ≤ W ≤ 100,000)์™€ ํ•ด๋‹น ๋ฌผ๊ฑด์˜ ๊ฐ€์น˜ V(0 ≤ V ≤ 1,000)

www.acmicpc.net

ํ’€์ด

์ฒ˜์Œ์—๋Š”, DP๋ฅผ ์–ด๋–ป๊ฒŒ ์ด์šฉํ•ด์•ผํ• ์ง€ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ ์ผ๋‹จ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค. 

๊ฒฐ๊ณผ๋Š” ๋„ˆ๋ฌด ๋‹น์—ฐํ•˜๊ฒŒ๋„ ์‹œ๊ฐ„์ดˆ๊ณผ์˜€๋‹ค! 

์™„์ „ํƒ์ƒ‰์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ๋‚˜๋ฉด, DP์— ๋Œ€ํ•œ ๊ฐ์ด ์˜ฌ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ „ํ˜€ ์˜ค์ง€ ์•Š์•˜๋‹ค. 

 

๊ฒฐ๊ตญ ๊ฒ€์ƒ‰์˜ ํž˜์„ ๋นŒ๋ ธ๋Š”๋ฐ, ์ด ๋ฌธ์ œ๋Š” ๋Œ€ํ‘œ์ ์ธ "๋ƒ…์ƒ‰ ๋ฌธ์ œ"๋ผ๊ณ  ํ•œ๋‹ค.

๋ƒ…์ƒ‰ ๋ฌธ์ œ(knapsack problem)๋ž€?

๋ƒ…์ƒ‰ ๋ฌธ์ œ๋ฅผ ์•Œ์•„๋“ฃ๊ธฐ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•˜๋ฉด, "๋ฐฐ๋‚ญ ์ฑ„์šฐ๊ธฐ ๋ฌธ์ œ"์ด๋‹ค. 

์ฆ‰, ์ฃผ์–ด์ง„ ์กฐ๊ฑด ํ•˜์—์„œ ์ตœ๋Œ€ํ•œ์œผ๋กœ ๋ฐฐ๋‚ญ์„ ์ฑ„์šฐ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ์— ๋Œ€ํ•œ ๋ฌธ์ œ์ด๋‹ค.

 

ํ’€์ด๋ฒ•

  • ์™„์ „ํƒ์ƒ‰
    • ๋ณต์žก๋„ : O(N^2) -> ์‹œ๊ฐ„์ดˆ๊ณผ ๊ฐ€๋Šฅ์„ฑ
  • Greedy 
    • ํ•ญ์ƒ ์ตœ์ ์˜ ๋‹ต์ด๋ผ๋Š” ๋ณด์žฅ์ด ์—†๋‹ค
  • DP
    • ์–ด๋–ค ๋ฌธ์ œ์— DP๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ฌธ์ œ์— ์ตœ์ ์˜ ์›๋ฆฌ๊ฐ€ ์„ฑ๋ฆฝ๋˜์–ด์•ผ ํ•œ๋‹ค.
      • ์ตœ์ ์˜ ์›๋ฆฌ? : ์–ด๋– ํ•œ ๊ฒฝ์šฐ์˜ ์ตœ์ ์˜ ํ•ด๊ฐ€, ์ด ๊ฒฝ์šฐ๋ฅผ ๋ถ„ํ• ํ•œ ๋ถ€๋ถ„ ์‚ฌ๋ก€์— ๋Œ€ํ•œ ์ตœ์ ํ•ด๋ฅผ ํ•ญ์ƒ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉด, ๊ทธ ๋ฌธ์ œ์— ๋Œ€ํ•ด ์ตœ์ ์˜ ์›๋ฆฌ๊ฐ€ ์„ฑ๋ฆฝํ•œ๋‹ค. 
      • ์ด ์›๋ฆฌ๋ฅผ ๋ƒ…์ƒ‰ ๋ฌธ์ œ์— ์ ์šฉํ•˜๋ฉด, 
        • ์ง‘ํ•ฉ A : ๋ƒ…์ƒ‰ ๋ฌธ์ œ์˜ ์ตœ์ ์˜ ํ•ด์—์„œ ๊ฐ€๋ฐฉ์— ๋‹ด๊ธด ๋ฌผ๊ฑด๋“ค์˜ ์ง‘ํ•ฉ
        • ์ง‘ํ•ฉ A๊ฐ€ n๋ฒˆ์งธ ๋ฌผ๊ฑด์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, 
          • A๋Š” n๋ฒˆ์งธ ๋ณด์„์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ n-1๊ฐœ์˜ ๋ฌผ๊ฑด๋“ค ์ค‘์— ์ตœ์ ์œผ๋กœ ๊ณ ๋ฅธ ๋ถ€๋ถ„์ง‘ํ•ฉ๊ณผ ๊ฐ™๋‹ค. 
        • ์ง‘ํ•ฉ A๊ฐ€ n๋ฒˆ์งธ ๋ฌผ๊ฑด์„ ํฌํ•จํ•œ๋‹ค๋ฉด,
          • A์— ์†ํ•œ ๋ฌผ๊ฑด๋“ค์˜ ์ด ๊ฐ€๊ฒฉ = n-1๊ฐœ์˜ ๋ฌผ๊ฑด๋“ค ์ค‘์—์„œ ์ตœ์ ์œผ๋กœ ๊ณ ๋ฅธ ๊ฐ€๊ฒฉ์˜ ํ•ฉ + n๋ฒˆ์งธ ๋ฌผ๊ฑด์˜ ๊ฐ€๊ฒฉ 
      • ์ด๊ฒƒ์„ ์ ํ™”์‹์œผ๋กœ ํ’€์–ด์“ฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
        • P[i, w] : i๊ฐœ์˜ ๋ณด์„, ๊ฐ€๋ฐฉ์˜ ๋ฌด๊ฒŒ ํ•œ๋„ w ์ผ ๋•Œ ์ตœ๋Œ€์˜ ์ด์ต

์™„์ „ํƒ์ƒ‰ ์ฝ”๋“œ (์‹œ๊ฐ„์ดˆ๊ณผ) 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int k,n;
int maxV = 0;
vector<vector<int>> objList;
void func(int start, int wSum, int vSum) {
	for (int i = start;i < n;i++) {
		if (wSum + objList[i][0] > k) {
			maxV = max(maxV, vSum);
			continue;
		}
		func(i, wSum + objList[i][0], vSum + objList[i][1]);
	}
}
int main(void) {
	//n: ๋ฌผ๊ฑด์˜ ๊ฐœ์ˆ˜, k:๊ฐ€๋ฐฉ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ 
	cin >> n >> k;

	for (int i = 0;i < n;i++) {
		int w, v;
		cin >> w >> v;
		objList.push_back({ w,v });
	}
	func(0, 0, 0);
	cout << maxV << endl;

	return 0;
}

 

DP ์ฝ”๋“œ (์„ฑ๊ณต!)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(void) {
	//n: ๋ฌผ๊ฑด์˜ ๊ฐœ์ˆ˜, k:๊ฐ€๋ฐฉ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ
	int k, n;
	vector<vector<int>> objList;
	
	cin >> n >> k;
	vector<vector<int>> cache(n+1, vector<int> (k+1));
	objList.push_back({0,0});
	for (int i = 0;i < n;i++) {
		int w, v;
		cin >> w >> v;
		objList.push_back({ w,v });
	}
	for (int i = 1;i <= n;i++) {
		for (int j = 0;j <= k;j++) {
			int weight = objList[i][0];
			if (j < weight)
				cache[i][j] = cache[i-1][j];
			else
				cache[i][j] = max(cache[i - 1][j], cache[i - 1][j - weight] + objList[i][1]);

		}
	}
	cout << cache[n][k];
	return 0;
}

 

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•