【洛谷P1049】装箱问题

装箱问题


Description

有一个箱子容量为 V V V(正整数,0 ≤ V V V ≤200000),同时有 n n n个物品(0< n n n≤30,每个物品有一个体积(正整数))。
要求 n n n个物品中,任取若干个装入箱内,使箱子的剩余空间为最小。

Input

1个整数,表示箱子容量

11个整数,表示有 n n n个物品

接下来 n n n行,分别表示这 n n n个物品的各自体积

Output

1个整数,表示箱子剩余空间。

Sample Input

24
6
8
3
12
7
9
7

Sample Output

0

Hink

NOIp2001普及组 第4题

解题思路

用搜索看装进哪个箱子比较好

#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=101;
int a[maxn],n,m,t;
void DFS(int dep,int s)
{
	if(s>m) return;//如果大于总数,退出
	if(dep>n)
	{
		t=max(t,s);//求最优方案
		return;
	}
	DFS(dep+1,s+a[dep]);
	DFS(dep+1,s);
}
int main()
{
	cin>>m>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	DFS(0,0);
	cout<<m-t;//输出剩下的空间
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值