알고리즘/BOJ
2662 기업투자 DP (코드만)
헤옹스
2018. 2. 20. 00:28
2662 기업투자
DP도 틀이 정해져있어요. 공식같은것
-
-
-그 다음으로 넘어가는 for문
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include<iostream> #include<cstring> #include<stack> using namespace std; const int MONEY_MAX = 300; const int CORP_MAX = 20; int arr[MONEY_MAX + 1][CORP_MAX + 1]; int money, corp; int cache[MONEY_MAX + 1][CORP_MAX + 1]; int ans; int output[MONEY_MAX + 1][CORP_MAX + 1]; int DP(int remain, int corpIdx) { if (corpIdx == corp + 1) return 0; if (remain == 0) return 0; int &ret = cache[remain][corpIdx]; if (ret != -1) return ret; ret = 0; for (int i = 0; i <= remain; i++) { int tmp = DP(remain - i, corpIdx) + arr[i][corpIdx]; if (tmp > ret) { ret = tmp; output[remain][corpIdx] = i; } return ret; } } int main() { int tmp; memset(cache, -1, sizeof(cache)); cin >> money >> corp; for (int i = 0; i <= money; i++) { cin >> tmp; for (int j = 0; j <= corp; j++) { cin >> arr[i][j]; } } ans = DP(money, 1); cout << ans << endl; for (int i = 1; i <= corp; i++) { cout << output[money][i] << " "; money -= output[money][i]; } return 0; } | cs |