[HDU - 2546] 饭卡 (01背包)

链接

https://www.cnblogs.com/pblr/p/4868375.html

题意

你有一张奇怪的饭卡,只要你的卡余额大于等于 5 5 5元,你可以购买任意价格的菜,即使会使余额变成负的,现在你的余额为 m m m,有 n n n种菜系,问你的余额最少可以为多少;

分析

  给所有菜系按价格升序排个序,先考虑使用 m − 5 m-5 m5元购买前 n − 1 n-1 n1种菜系最大可以花掉多少钱(这里使用01背包),之后在买价格最贵的第 n n n种菜系;

代码
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

#define INF 0x7f7f7f7f
#define MAXN 100005
#define N 200005
#define P 2
#define MOD 99991

typedef long long ll;

namespace fastIO {
	//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
	//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
	inline int read() {
		char c = getchar(); int x = 0, f = 1;
		while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
}

using namespace fastIO;
using namespace std;

int n, m, dp[1004], a[1005];

int main() {
	while (cin >> n, n) {
		for (int i = 1; i <= n; i++)
			cin >> a[i];
		memset(dp, 0, sizeof(dp));
		cin >> m;
		sort(a + 1, a + 1 + n);
		for (int i = 1; i <= n - 1; i++)
			for (int j = m - 5; j >= a[i]; j--) {
				dp[j] = max(dp[j], dp[j - a[i]] + a[i]);
			}
		cout << m - dp[m - 5] - a[n] << endl;
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值