Skip to content

Instantly share code, notes, and snippets.

@lawrence910426
Created April 27, 2022 15:43
Show Gist options
  • Save lawrence910426/c35dd33d7967cea24d47c8ba0e5d9715 to your computer and use it in GitHub Desktop.
Save lawrence910426/c35dd33d7967cea24d47c8ba0e5d9715 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <memory.h>
using namespace std;
int dp[6][6][6][6][6];
int promo_amount[5][100], promo_sum[100], amount[5], price[5];;
int id_mapping[1000];
int id = 0;
inline int min(int a, int b) { return (a > b ? b : a); }
void update(
int _1, int _2, int _3, int _4, int _5,
int inc1, int inc2, int inc3, int inc4, int inc5,
int priceIncrement
) {
int __1 = _1 + inc1,
__2 = _2 + inc2,
__3 = _3 + inc3,
__4 = _4 + inc4,
__5 = _5 + inc5;
// Margin check
if (__1 >= 6 || __2 >= 6 || __3 >= 6 || __4 >= 6 || __5 >= 6) return;
if (dp[_1][_2][_3][_4][_5] == -1) return;
if (dp[__1][__2][__3][__4][__5] == -1)
dp[__1][__2][__3][__4][__5] = dp[_1][_2][_3][_4][_5] + priceIncrement;
else
dp[__1][__2][__3][__4][__5] = min(
dp[_1][_2][_3][_4][_5] + priceIncrement,
dp[__1][__2][__3][__4][__5]
);
}
int main() {
int N, M, i, j, k, ii, jj, kk, iii;
memset(id_mapping, -1, sizeof(id_mapping));
memset(promo_amount, 0, sizeof(promo_amount));
memset(dp, -1, sizeof(dp));
memset(amount, 0, sizeof(amount));
cin >> N;
for(i = 0; i < N; i++) {
int c, k, p;
// code, amount, price.
cin >> c >> k >> p;
// map code to sequential id
if(id_mapping[c] == -1)
id_mapping[c] = id++;
// use the mapped id as index
amount[id_mapping[c]] = k;
price[id_mapping[c]] = p;
}
cin >> M;
for(i = 0; i < M; i++) {
int s, t;
cin >> s;
for(j = 0; j < s; j++) {
int c, h;
cin >> c >> h;
// use the mapped id as index
promo_amount[id_mapping[c]][i] = h;
}
cin >> t;
promo_sum[i] = t;
}
dp[0][0][0][0][0] = 0;
for(i = 0; i <= 5; i++) for(j = 0; j <= 5; j++) for(k = 0; k <= 5; k++)
for(ii = 0; ii <= 5; ii++) for(jj = 0; jj <= 5; jj++) {
// update by a single commodity
for(kk = 0; kk < id; kk++)
update(
i, j, k, ii, jj,
kk == 0, kk == 1, kk == 2, kk == 3, kk == 4,
price[kk]
);
// update by a pack of commodity
for(kk = 0; kk < M; kk++)
update(
i, j, k, ii, jj,
promo_amount[0][kk],
promo_amount[1][kk],
promo_amount[2][kk],
promo_amount[3][kk],
promo_amount[4][kk],
promo_sum[kk]
);
}
// Show the entire dp table (commented)
/*
for(i = 0; i <= 5; i++) for(j = 0; j <= 5; j++) for(k = 0; k <= 5; k++)
for(ii = 0; ii <= 5; ii++) for(jj = 0; jj <= 5; jj++)
cout << i << " " << j << " " << k << " " << ii << " " << jj << " " <<
dp[i][j][k][ii][jj] << endl;
*/
cout << dp
[amount[0]]
[amount[1]]
[amount[2]]
[amount[3]]
[amount[4]] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment