当前位置:网站首页>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;
}
边栏推荐
- Chrome officially supports MathML, which is enabled in chromium dev 105 by default
- 微信核酸检测预约小程序系统毕业设计毕设(4)开题报告
- [golang | grpc] generate certificates using OpenSSL
- UE4 用spline畫正圓
- Installation tutorial and simple call of Matplotlib
- 实施阴影介绍
- ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null
- Renren potential field method
- 微信核酸检测预约小程序系统毕业设计毕设(2)小程序功能
- Iframe nesting details
猜你喜欢

Leetcode 面试题 16.15. 珠玑妙算

Leetcode 面试题 16.11. 跳水板

Leetcode interview question 17.01 Addition without plus sign

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

RDK仿真实验

巴比特 | 元宇宙每日必读:一千块就能买一个虚拟主播?这是小企业的直播福音还是在“割韭菜”?...

再放寬!這些應届生,可直接落戶上海

Leetcode interview question 17.04 Vanishing numbers

Troubleshooting ideas that can solve 80% of faults

In early summer, Kaiyuan magic changed an electric mosquito racket with killing sound effect!
随机推荐
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
Unity learning shader notes [81] simple color adjustment post-processing (brightness, saturation, contrast)
qt的内存映射
RDK simulation experiment
QQmlApplicationEngine
【Oracle 期末复习】表空间、表、约束、索引、视图的增删改
Unity学习shader笔记[八十一]简单的颜色调整后处理(亮度,饱和度,对比度)
NM01-独立于总线协议的NM模块功能概述与API定义
Three methods of MySQL backup
再放宽!这些应届生,可直接落户上海
Esp32-c3 introductory tutorial question ⑪ - ESP tls: create_ ssl_ handle failed, tls_ io_ instance->options. trusted_ certs null
2020 Internet industry terminology
A good programmer is worth five ordinary programmers!
好玩的免费GM游戏整理汇总
Export Excel files using npoi
Redis(7)----数据库与过期键
Design of the multi live architecture in different places of the king glory mall
The official docker image running container in version 1.5.1 can be set to use MySQL 8 driver?
1.5.1版本官方docker镜像运行容器,能设置使用 mysql 8驱动吗?
微信小程序视频分享平台系统毕业设计毕设(8)毕业设计论文模板