P1049 [NOIP2001 普及组] 装箱问题
现在的我
#include<iostream>
#include<algorithm>
using namespace std;
int dp[50][20005] = {0};
int main(void){
int V, n;
int v[50];
cin >> V >> n;
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n; i++){
for (int j = 0; j <= V; j++){
dp[i][j] = dp[i - 1][j];
if (j >= v[i]) dp[i][j] = max(dp[i][j], dp[i-1][j - v[i]]+ v[i]);
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= V; j++){
// printf("%d ", dp[i][j]);
}
// cout << endl;
}
cout << V - dp[n][V];
return 0;
}
以前的我
(现在的我菜了orz)
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 20005;
int v[N];
int dp[N];
int main() {
int V,n;
cin>> V>> n;
for (int i = 1; i <= n; i++) {
cin>> v[i];
}
for (int i = 1; i <= n; i++) {
for (int k = V; k >= 1; k--) {
if (k-v[i]>=0)
dp[k] = max (dp[k],dp[k-v[i]]+v[i]);
else if (k-v[i]<0)
dp[k] = dp[k];
//cout << dp[i][k]<<" ";
}
//cout <<endl;
}
cout<< (V-dp[V]);
return 0;
}