A-Potato Sacks---2018纽约区域赛(01背包裸题)

博客围绕Potato Sacks题目展开,给出题目链接、描述、输入输出要求及示例。题目是给定多组数据,每组含背包大小和多个马铃薯重量,询问能否恰好装满背包。指出这是01背包裸题,可将重量和价值等同,通过判断dp[v]是否等于v求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Potato Sacks

Time Limit: 1 Sec Memory Limit: 128 Mb
题目链接http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2270

Description

Potato sacks come in different weight capacities (specified in pounds). Potatoes come in different weights. If you are given some number of potatoes of possibly different weights (specified in pounds), determine if it is possible to exactly fill a potato sack of a given capacity using some or all of the potatoes.
在这里插入图片描述

Input

The first line of input contains a single decimal integer P,(1 ≤ P ≤ 100), which is the number of data sets that follow. Each data set should be processed identically and independently. Each data set consists of a single line of input containing 12 space separated positive integers. They are the data set number, K, followed by the capacity, C, of the potato sack in pounds, 10 ≤ C ≤ 30, followed by the weights of 10 potatoes in pounds. A potato will not weigh more than 3 pounds.

Output

For each data set there is a single line of output. The output line consists of the data set number,K, followed by a single space, the word YES if the potato sack can be filled exactly to capacity C pounds or the word NO if it cannot be filled exactly

Sample Input

2
1 20 3 2 1 3 3 2 3 2 1 1
2 25 3 3 3 3 3 3 3 3 3 3

Sample Output

1 YES
2 NO


题目大意是这样的:给你t组数据每行12个数字,第一个是编号,第二个是背包大小,接下来十个是马铃薯的重量,询问是否有一种方法能恰好装满背包。
emmm。。。这题没什么好说的,01背包裸题,学过01背包的应该知道怎么做了,没学过的可以看我普通dp专题里的01背包哦(* ^ ▽^ *)。我们将重量和价值画个等于号就行了,最后判断dp[v]是否等于v就行了。

#include <cstdio>
#include <cstring>
using namespace std;
int a[15], c[40];
int max(int a, int b)
{
	return a > b ? a : b;
}
int main()
{
	int t, n, id, v;
	scanf("%d", &t);
	for (int o = 1; o <= t; o++) {
		scanf("%d%d", &id, &v);
		memset(c,0,sizeof(c));
		for (int i = 1; i <= 10; i++) {
			scanf("%d", &a[i]);
		}
		printf("%d ", o);
		for (int i = 1; i <= 10; i++) {
			for (int j = v; j >= a[i]; j--)
				c[j] = max(c[j], c[j - a[i]] + a[i]);
		}
		if (c[v] == v) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值