链接
https://www.cnblogs.com/pblr/p/4868375.html
题意
你有一张奇怪的饭卡,只要你的卡余额大于等于 5 5 5元,你可以购买任意价格的菜,即使会使余额变成负的,现在你的余额为 m m m,有 n n n种菜系,问你的余额最少可以为多少;
分析
给所有菜系按价格升序排个序,先考虑使用 m − 5 m-5 m−5元购买前 n − 1 n-1 n−1种菜系最大可以花掉多少钱(这里使用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;
}
}