当前位置:网站首页>饭卡 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;
}
边栏推荐
- 微信小程序视频分享平台系统毕业设计毕设(3)后台功能
- No such file or directory: ‘/tmp/tmpxxx/tmpxxx. py‘
- Enter a valid user name and password in the Microsoft LDAP configuration page, and enter a valid user name in the Microsoft LDAP configuration page
- 2020互联网行业术语
- Wechat nucleic acid detection appointment applet system graduation design completion (1) development outline
- 719. Find the distance of the number pair with the smallest K
- Pit encountered during installation of laravel frame
- QQmlApplicationEngine
- [golang | grpc] generate certificates using OpenSSL
- MySQL advanced - transaction and index
猜你喜欢

Wechat applet video sharing platform system graduation design completion (5) assignment

Leetcode 面试题 17.04. 消失的数字

Wechat applet video sharing platform system graduation design completion (1) development outline

In Linux, MySQL sets the job task to start automatically

How to download wechat payment certificate (API certificate)

Pychar modify pep8 e501 line too long > 0 characters

又一所双非改考408,会爆冷么?南昌航空大学软件学院

Another double non reform exam 408, will it be cold? Software College of Nanchang Aviation University

Wechat applet video sharing platform system graduation design (2) applet function

Editor编辑器扩展在Scene View添加按钮和logo
随机推荐
Android cycle timer implementation, to achieve fixed Android cache cleaning
wait_ for_ Gap -- restore archive from primary archive to secondary Archive
毕业总结
Leetcode 面试题 16.17. 连续数列
RDK simulation experiment
微信小程序视频分享平台系统毕业设计毕设(7)中期检查报告
C# 检测图片是否被旋转并修改到正真的旋转
Finally detailed explanation
国金证券是国企吗?在国金证券开户资金安全吗?
Explain kubernetes network model in detail
MySQL -- basic concept of database
切换变换的时候记得使用三元表达式
Chrome officially supports MathML, which is enabled in chromium dev 105 by default
Detailed explanation of map set
Détends - toi encore! Ces nouveaux étudiants peuvent s'installer directement à Shanghai
Wechat applet video sharing platform system graduation design completion (5) assignment
Wechat applet video sharing platform system graduation design completion (7) Interim inspection report
自定义一个loading指令
Use dosbox to run the assembly super detailed step "suggestions collection"
Unity学习shader笔记[八十二]增强单通道颜色渲染的黑白处理