当前位置:网站首页>Meal card hdu2546
Meal card hdu2546
2022-07-02 18:29:00 【EdwinAze】
meal card HDU2546
meal card - HDU 2546 - Virtual Judge (csgrandeur.cn)
There is a very strange design of the dining card in the canteen of the University of Electronic Science and technology , That is to judge the balance before purchasing . If you buy a product before , The remaining amount on the card is greater than or equal to 5 element , You can buy it successfully ( Even if the card balance is negative after purchase ), Otherwise you can't buy ( Even if the amount is enough ). So we all hope to minimize the balance on the card .
One day , There are in the canteen n Grow vegetables and sell , Each dish can be purchased once . Know the price of each dish and the balance on the card , Ask the minimum balance on the card .
Input
Multiple sets of data . For each set of data :
The first line is a positive integer n, Indicates the number of dishes .n<=1000.
The second line includes n A positive integer , Indicates the price of each dish . Price does not exceed 50.
The third line contains a positive integer m, Represents the balance on the card .m<=1000.
n=0 Indicates the end of the data .
Output
For each group of input , Output one line , Contains an integer , Indicates the minimum possible balance on the card .
Input
1
50
5
10
1 2 3 2 1 1 2 3 2 1
50
0
output
-45
32
Ideas
If you change the title to , Meal card balance m, n A dish , Try to buy dishes with the greatest total value , Then it's standard 01 knapsack problem .
But here is the minimum balance of the meal card , And the cost of this question is equal to the value , When the total value is the largest, the balance is the smallest , Subtract the total value from the total balance .
But there is also pawn The balance is Greater than 5 when , You can stuff large items beyond the capacity of your backpack , Then the optimal solution must be this 5 Buy the most expensive dishes with RMB , So I used the balance first 5 block , Buy the biggest one , The rest of the money and then ask for the maximum value of the remaining dishes .
The capacity of the backpack cannot overflow when it is left , That is, the standard 01 knapsack problem , Calculate the maximum value and add the original largest dish when exporting .
Code
#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;
}
边栏推荐
- [Oracle final review] addition, deletion and modification of tablespaces, tables, constraints, indexes and views
- MySQL installation and configuration
- 2020互联网行业术语
- 一个优秀程序员可抵五个普通程序员!
- Leetcode 面试题 17.04. 消失的数字
- [Yugong series] July 2022 go teaching course 001 introduction to go language premise
- SteamOS 3.3 Beta 发布,Steam Deck 中文键盘终于来了
- Pychar modify pep8 e501 line too long > 0 characters
- ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null
- Calculation of favorable comment rate
猜你喜欢

Wechat applet video sharing platform system graduation design (3) background function

微信小程序视频分享平台系统毕业设计毕设(7)中期检查报告

微信核酸检测预约小程序系统毕业设计毕设(1)开发概要

微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT

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

NVIDIA graphics card failed to initialize nvml driver/library version mismatch error solution
![[golang | grpc] use grpc to realize simple remote call](/img/93/ae6eba2ba65d6e23bd00f45d04a2ed.png)
[golang | grpc] use grpc to realize simple remote call

Leetcode interview question 16.17 Continuous sequence

如何开启IDEA的Run Dashboard功能

Détends - toi encore! Ces nouveaux étudiants peuvent s'installer directement à Shanghai
随机推荐
Embedded development board ~ description
【西北工业大学】考研初试复试资料分享
实施阴影介绍
Nm02 nm module call sequence diagram and code interpretation independent of bus protocol
Wechat nucleic acid detection and appointment applet system graduation design (3) background function
Another double non reform exam 408, will it be cold? Software College of Nanchang Aviation University
Intelligent hydropower meter energy consumption monitoring cloud platform
pycharm 修改 pep8 E501 line too long > 0 characters
paddlepaddle 28 搭建基于卷积的自动编码机
Wechat applet video sharing platform system graduation design completion (8) graduation design thesis template
Detailed explanation of map set
vimium映射键
Implementation shadow introduction
再放寬!這些應届生,可直接落戶上海
UE4 用spline畫正圓
Relax again! These fresh students can settle directly in Shanghai
揭秘得物客服IM全链路通信过程
Aptos教程-参与官方激励测试网(AIT2 激励测试网)
Iframe nesting details
求求你们,别再刷 Star 了!这跟“爱国”没关系!