P1049 [NOIP2001 普及组 T4] 装箱问题

【题目来源】
https://www.luogu.com.cn/problem/P1049
https://www.acwing.com/problem/content/1026/

【题目描述】
有一个箱子容量为 V,同时有 n 个物品,每个物品有一个体积(正整数)。
要求 n 个物品中,任取若干个装入箱内,使箱子的剩余空间最小

【输入格式】
第一行是一个整数 V,表示箱子容量。
第二行是一个整数 n,表示物品数。
接下来 n 行,每行一个正整数(不超过10000),分别表示这 n 个物品的各自体积。

【输出格式】
一个整数,表示箱子剩余空间。

【数据范围】
0<V≤20000,0<n≤30

【算法代码】

#include <bits/stdc++.h>
using namespace std;
 
const int maxv=20005,maxn=35;
int vol[maxn];
int c[maxn][maxv];
//c[i][j]: The previous i items which total volume does not exceed j
 
int main() {
	int V,n;
	cin>>V>>n;
	for(int i=1; i<=n; i++) cin>>vol[i];
 
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=V; j++) {
			c[i][j]=c[i-1][j]; //Don't select current item
			if(j>=vol[i]) c[i][j]=max(c[i][j],c[i-1][j-vol[i]]+vol[i]);
			//Select the current item and the volume must be sufficient
		}
	}
	cout<<V-c[n][V]<<endl; //Free space
 
	return 0;
}
 
/*
in:
24
6
8
3
12
7
9
7

out:
0
*/


【参考文献】
https://www.acwing.com/solution/content/91279/


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值