1. 程式人生 > 其它 >P1049 [NOIP2001 普及組] 裝箱問題

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;
}