当前位置:网站首页>饭卡 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;
}
边栏推荐
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- 一个优秀程序员可抵五个普通程序员!
- Wechat nucleic acid detection appointment applet system graduation design completion (5) task statement
- Leetcode 面试题 16.11. 跳水板
- 巴比特 | 元宇宙每日必读:一千块就能买一个虚拟主播?这是小企业的直播福音还是在“割韭菜”?...
- Relax again! These fresh students can settle directly in Shanghai
- Freemaker+poi realizes dynamic generation and parsing of Excel files
- em120.gige. h
- A4988与42步进电机
- Wechat nucleic acid detection appointment applet system graduation design completion (4) opening report
猜你喜欢
MySQL安装与配置
微信核酸检测预约小程序系统毕业设计毕设(2)小程序功能
Ora-19838 -- restore control files to the standby database
一个优秀程序员可抵五个普通程序员!
Qt Official examples: Qt Quick Controls - Gallery
Wechat applet video sharing platform system graduation design completion (4) opening report
Wechat applet video sharing platform system graduation design (2) applet function
微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT
Detailed explanation of map set
Pychar modify pep8 e501 line too long > 0 characters
随机推荐
Wechat applet video sharing platform system graduation design completion (4) opening report
1.5.1版本官方docker镜像运行容器,能设置使用 mysql 8驱动吗?
Wechat applet video sharing platform system graduation design (2) applet function
Wasserstein slim gain with clipping penalty (wsgain-cp) introduction and code implementation -- missing data filling based on generating countermeasure network
自定义一个loading指令
Web chat tool
wait_ for_ Gap -- restore archive from primary archive to secondary Archive
MySQL --- 数据库的基本操作
WPS inserts a picture and displays it completely
Qt官方示例:Qt Quick Controls - Gallery
Clé de cartographie vimium
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
求求你们,别再刷 Star 了!这跟“爱国”没关系!
Wechat nucleic acid detection appointment applet system graduation design completion (5) task statement
Picking up the camera is the best artistic healing
Yingguang MCU development case
1288_ Implementation analysis of vtask resume() interface and interrupt Security version interface in FreeRTOS
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
Ora-19838 -- restore control files to the standby database
Android cycle timer implementation, to achieve fixed Android cache cleaning