-
algospot - packingAlgorithm/알고스팟 2020. 2. 10. 09:5412345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273//algospot - packing#include <iostream>#include <vector>using namespace std;int N, W;vector<string> name;vector<int> vol;vector<int> pre;vector<vector<int>> cache;vector<int> list;int num;void init(){list = vector<int>();num = 0;cin >> N >> W;name = vector<string>(N);vol = pre = vector<int>(N);for (int i = 0; i < N; ++i)cin >> name[i] >> vol[i] >> pre[i];cache = vector<vector<int>>(N, vector<int>(W + 1, -1));}int dp(int idx, int capa){if (idx == N)return 0;int &ret = cache[idx][capa];if (ret != -1)return ret;ret = dp(idx + 1, capa);if (capa >= vol[idx])ret = max(ret, dp(idx + 1, capa - vol[idx]) + pre[idx]);return ret;}void reconstruct(int idx, int capa){int t = cache[idx][capa];if (idx == N - 1){if (t)list.push_back(idx);return;}if (cache[idx + 1][capa] == t)reconstruct(idx + 1, capa);else{list.push_back(idx);reconstruct(idx + 1, capa - vol[idx]);}}int main(){int C;cin >> C;for (int tn = 0; tn < C; ++tn){init();cout << dp(0, W) << " ";reconstruct(0, W);cout << list.size() << "\n";for(int i = 0; i<list.size(); ++i)cout << name[list[i]] << "\n";}}
cs https://algospot.com/judge/problem/read/PACKING
algospot.com :: PACKING
여행 짐 싸기 문제 정보 문제 여행을 떠나기 전날까지 절대 짐을 싸지 않는 버릇이 있는 재훈이는 오늘도 비행기 타기 전날에야 가방을 싸기 위해 자리에 앉았습니다. 비행기 규정상 재훈이는 캐리어를 하나만 가지고 갈 수 있는데, 아무래도 가져가고 싶은 물건들이 캐리어 안에 다 들어가지 않을 것 같습니다. 재훈이는 가져가고 싶은 각 물건들의 부피와 얼마나 필요한지를 나타내는 절박도를 조사해 다음과 같은 목록을 만들었습니다. 물건 노트북 컴퓨터 카메라 XBOX3
algospot.com
PACKING
DP 실제 답 구하는 문제
reconstruct() 재귀함수를 만들어 추적해줍니다!

'Algorithm > 알고스팟' 카테고리의 다른 글
algospot - matchfix (0) 2020.02.17 algospot - morse (0) 2020.02.10 algospot - snail (0) 2020.02.08 algospot - numb3rs (0) 2020.02.07 algospot - poly (0) 2020.02.07