LCS没学过,稍微记一下。
i
f
(
s
[
i
]
=
=
s
[
j
]
)
if(s[i]==s[j])
if(s[i]==s[j])
f
[
i
]
[
j
]
=
m
a
x
(
f
[
i
−
1
]
[
j
−
1
]
+
1
,
f
[
i
]
[
j
]
)
f[i][j]=max(f[i-1][j-1]+1,f[i][j])
f[i][j]=max(f[i−1][j−1]+1,f[i][j])
e
l
s
e
else
else
f
[
i
]
[
j
]
=
m
a
x
(
f
[
i
−
1
]
[
j
]
,
f
[
i
]
[
j
−
1
]
)
f[i][j] = max(f[i-1][j],f[i][j-1])
f[i][j]=max(f[i−1][j],f[i][j−1])
金明的预算方案,算是分组背包的有限变形吧。
#include <bits/stdc++.h>
using namespace std;
const int N = 100,M = 3.2 * 1e4;
int cost[N][4][2];
bool num[N];
long long f[M];
int main(){
int n,m;
cin >> n >> m;
for(int i = 1;i <= m;i ++){
int a,b,c;
cin >> a >> b >> c;
if(! c){
cost[i][1][0] = a;
cost[i][1][1] = a * b;
}else{
if(! num[c]){
cost[c][2][0] = a;
cost[c][2][1] = a * b;
num[c] = true;
}else{
cost[c][3][0] = a;
cost[c][3][1] = a * b;
}
}
}
for(int i = 1;i <= m;i ++){
if(! cost[i][1][0]) continue;
for(int j = n;j >= 1;j --){
if(j >= cost[i][1][0]) f[j] = max(f[j],f[j - cost[i][1][0]] + cost[i][1][1]);
if(j >= cost[i][1][0] + cost[i][2][0]) f[j] = max(f[j],f[j - cost[i][1][0] - cost[i][2][0]] + cost[i][1][1] + cost[i][2][1]);
if(j >= cost[i][1][0] + cost[i][3][0]) f[j] = max(f[j],f[j - cost[i][1][0] - cost[i][3][0]] + cost[i][1][1] + cost[i][3][1]);
if(j >= cost[i][1][0] + cost[i][2][0] + cost[i][3][0]) f[j] = max(f[j],f[j - cost[i][1][0] - cost[i][2][0] - cost[i][3][0]] + cost[i][1][1] + cost[i][2][1] + cost[i][3][1]);
}
}
cout << f[n];
return 0;
}