-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path638.cpp
More file actions
78 lines (65 loc) · 1.99 KB
/
Copy path638.cpp
File metadata and controls
78 lines (65 loc) · 1.99 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Solution {
public:
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
int n = price.size();
if(n==0)return 0;
int sz = dec[n];
int* dp = new int[sz];
dp[0] = 0;
int* tmp = new int[n];
for(int i = 1; i < sz; ++i)
{
int mi = 0;
//计算每个物品的数量
bool flag = false;
int sum = i;
for(int j = 0; j < n; ++j)
{
tmp[j] = sum % 7;
if(tmp[j] > needs[j]){flag = true;break;}
sum -= tmp[j];
sum /= 7;
mi += tmp[j] * price[j];
//cout << tmp[j] << "|";
}
if(flag)continue;
//逐个大礼包尝试
for(int k = 0; k < special.size(); ++k)
{
bool flag = false;
for(int l = 0; l < n; ++l)
{
if(special[k][l] > tmp[l])
{
flag = true;
break;
}
}
if(flag)continue;
int m = 0;
for(int j = n-1; j >= 1; --j)
{
m += special[k][j];
m *= 7;
}
m += special[k][0];
if(special[k][n] + dp[i-m] < mi)
{
mi = special[k][n] + dp[i-m];
}
}
dp[i] = mi;
//cout << mi << endl;
}
int m = 0;
for(int j = n-1; j >= 1; --j)
{
m += needs[j];
m *= 7;
}
m += needs[0];
//cout << "m:" << m << endl;
return dp[m];
}
int dec[7] = {1,7,49,343,2401,16807,117649};
};