当前位置:网站首页>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;
}
边栏推荐
- Détends - toi encore! Ces nouveaux étudiants peuvent s'installer directement à Shanghai
- Wechat nucleic acid detection appointment applet system graduation design completion (4) opening report
- Rte11 interrupt decoupling function
- Troubleshooting ideas that can solve 80% of faults
- 好玩的免费GM游戏整理汇总
- 微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT
- Nm02 nm module call sequence diagram and code interpretation independent of bus protocol
- How can you omit a large number of switch statements
- UE4 用spline画正圆
- [Oracle final review] addition, deletion and modification of tablespaces, tables, constraints, indexes and views
猜你喜欢

Explain kubernetes network model in detail

好玩的免费GM游戏整理汇总

UE4 draw a circle with spline

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

Wechat applet video sharing platform system graduation design completion (4) opening report

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

NM01-独立于总线协议的NM模块功能概述与API定义

Summary of fun free GM games

Wechat applet video sharing platform system graduation design completion (8) graduation design thesis template
![Unity learning shader notes [82] black and white processing of enhanced single channel color rendering](/img/db/d745a434e76511742d1264706b5d9a.png)
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
随机推荐
Wechat nucleic acid detection appointment applet system graduation design completion (5) task statement
paddlepaddle 28 搭建基于卷积的自动编码机
Wechat applet video sharing platform system graduation design (3) background function
Redis(7)----数据库与过期键
微信小程序视频分享平台系统毕业设计毕设(4)开题报告
Is Guojin securities a state-owned enterprise? Is it safe to open an account in Guojin securities?
C # detect whether the picture is rotated and modified to the true rotation
qt的内存映射
人人工势场法
Wechat applet video sharing platform system graduation design completion (7) Interim inspection report
719. Find the distance of the number pair with the smallest K
Leetcode 面试题 17.04. 消失的数字
Web聊天工具
matplotlib的安装教程以及简单调用
Détends - toi encore! Ces nouveaux étudiants peuvent s'installer directement à Shanghai
夜神模拟器+Fiddler抓包测试App
Qt官方示例:Qt Quick Controls - Gallery
Leetcode interview question 16.15 Abacus wonderful calculation
QT official example: QT quick controls - Gallery
Leetcode interview question 16.17 Continuous sequence