当前位置:网站首页>饭卡 HDU2546
饭卡 HDU2546
2022-07-02 16:49:00 【EdwinAze】
饭卡 HDU2546
饭卡 - HDU 2546 - Virtual Judge (csgrandeur.cn)
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。
Input
多组数据。对于每组数据:
第一行为正整数n,表示菜的数量。n<=1000。
第二行包括n个正整数,表示每种菜的价格。价格不超过50。
第三行包括一个正整数m,表示卡上的余额。m<=1000。
n=0表示数据结束。
Output
对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。
Input
1
50
5
10
1 2 3 2 1 1 2 3 2 1
50
0
output
-45
32
思路
如果把题目改成, 饭卡余额m, n个菜品, 求买到菜品总价值最大,那么是标准的01背包问题。
但这里求饭卡余额最小, 且该题代价跟价值是相等的,总价值最大时余额便是最小, 用总余额减去总价值即可。
不过还有当 余额为 大于5时, 可以超出背包容量塞进大物品, 那么最优解肯定是用这5块买最贵的菜品, 于是就把余额先用去5块, 买最大的那个, 剩下的钱再求剩下的菜品最大价值。
求剩下时背包容量不能溢出, 也就是标准01背包问题, 求出最大价值在输出时加上原本最大的那个菜品就行。
代码
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e3 +10;
int f[N];
int w[N];
int n,m;
int main()
{
while(cin >> n && n)
{
memset(f, 0, sizeof f);
for(int i = 1; i <= n;i ++)
cin >> w[i];
sort(w + 1, w + 1 + n);
cin >> m;
if(m < 5)
{
cout << m <<endl;
continue;
}
for(int i = 1; i < n; i++)
for(int j = m - 5; j >= w[i]; j --)
f[j] = max(f[j], f[j - w[i]] + w[i]);
cout << m - a[n] - f[m - 5] << endl;
}
return 0;
}
边栏推荐
- Architecture design - ID generator "suggestions collection"
- 2020 Internet industry terminology
- Mysql - opérations de base de la base de données
- SteamOS 3.3 Beta 发布,Steam Deck 中文键盘终于来了
- Wechat applet video sharing platform system graduation design (3) background function
- QT official example: QT quick controls - Gallery
- win10 卸载cuda
- 好玩的免费GM游戏整理汇总
- 719. 找出第 K 小的数对距离
- Vimium mapping key
猜你喜欢
Remember to use ternary expressions when switching transformations
微信小程序视频分享平台系统毕业设计毕设(4)开题报告
Detailed explanation of map set
Microsoft LDAP 配置页中输入有效的用户名及密码,microsoft ldap 配置页中输入有效的用户名
Babbitt | metauniverse daily must read: can you buy a virtual anchor for 1000 yuan? Is this the live gospel of small businesses or "cutting leeks"
Wechat applet video sharing platform system graduation design completion (7) Interim inspection report
QT official example: QT quick controls - Gallery
RDK simulation experiment
[golang | grpc] use grpc to realize simple remote call
Wechat applet video sharing platform system graduation design completion (5) assignment
随机推荐
[Yugong series] July 2022 go teaching course 001 introduction to go language premise
719. Find the distance of the number pair with the smallest K
D constructor problem
Web聊天工具
Editor Editor Extension add button and logo in scene view
qt的内存映射
RTE11- 中断解耦功能
Customize a loading instruction
Chrome 正式支持 MathML,默认在 Chromium Dev 105 中启用
Nvidia 显卡 Failed to initialize NVML Driver/library version mismatch 错误解决方案
拿起相机,便是最好的艺术疗愈
Web chat tool
[golang | grpc] use grpc to realize simple remote call
win10 卸载cuda
Qt Official examples: Qt Quick Controls - Gallery
初夏,开源魔改一个带击杀音效的电蚊拍!
[golang | grpc] generate certificates using OpenSSL
Vimium mapping key
Unity学习shader笔记[八十一]简单的颜色调整后处理(亮度,饱和度,对比度)
Simple understanding of cardinality sorting