https://www.luogu.org/problem/P1049
题目描述
[NOIP2001 普及组] 装箱问题
题目描述
有一个箱子容量为 V V V,同时有 n n n 个物品,每个物品有一个体积。
现在从 n n n 个物品中,任取若干个装入箱内(也可以不取),使箱子的剩余空间最小。输出这个最小值。
输入格式
第一行共一个整数 V V V,表示箱子容量。
第二行共一个整数 n n n,表示物品总数。
接下来 n n n 行,每行有一个正整数,表示第 i i i 个物品的体积。
输出格式
- 共一行一个整数,表示箱子最小剩余空间。
样例 #1
样例输入 #1
24
6
8
3
12
7
9
7
样例输出 #1
0
提示
对于 100 % 100\% 100% 数据,满足 0 < n ≤ 30 0<n \le 30 0<n≤30, 1 ≤ V ≤ 20000 1 \le V \le 20000 1≤V≤20000。
【题目来源】
NOIP 2001 普及组第四题
idea
in my first eyes , I didn’t find it just a 01 bag problem . maybe a One dimension(维度) dp
after I saw the problem solution , it told me just a 01 bag problem , and the key of this problem is you should look the Volume as the Value .
after that , we can get the answer .
AC Code
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
using namespace std;
int a[20005],dp[20005];
int main(){
int n,num;
cin >> num >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
for(int i = 1;i <= n;i++)
for(int j = num;j > 0;j--){
if(j>=a[i])
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
}
cout << num-dp[num] << endl;
}