居然没有秒切的01背包,我还是太弱了的洛谷P1049 装箱问题

5 篇文章 0 订阅

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<n30 1 ≤ V ≤ 20000 1 \le V \le 20000 1V20000

【题目来源】

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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值