A - 饭卡
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。
Input 多组数据。对于每组数据:
第一行为正整数n,表示菜的数量。n<=1000。
第二行包括n个正整数,表示每种菜的价格。价格不超过50。
第三行包括一个正整数m,表示卡上的余额。m<=1000。
n=0表示数据结束。
Output 对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。
Sample Input
1
50
5
10
1 2 3 2 1 1 2 3 2 1
50
0
Sample Output
-45
32
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int main()
{
int n,ch[],m,sum;
while(scanf("%d",&n) && n!=)
{
sum=;
for(int i=; i<n; i++)
{
scanf("%d",&ch[i]);
sum+=ch[i];
}
scanf("%d",&m);
if(m<) {printf("%d\n",m); continue;}
if(sum<=m-) { printf("%d\n",m-sum); continue; }
sort(ch,ch+n);
int dp[];
memset(dp,,sizeof(dp));
for(int i=; i<n-; i++)
{
for(int j=m-;j>=ch[i];j--)
dp[j]=max(dp[j],dp[j-ch[i]]+ch[i]);
}
printf("%d\n",m-dp[m-]-ch[n-]);
}
return ;
}
A - 饭卡的更多相关文章
- (01背包 排序+特判)饭卡(hdu 2546)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额 ...
- hdu 2546 饭卡 删除一个数的01背包
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- DP入门---饭卡
HDU 2546 Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额 ...
- HDOJ 2546饭卡(01背包问题)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如 ...
- cdoj 31 饭卡(card) 01背包
饭卡(card) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/31 Des ...
- hdoj 2546 饭卡
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- HDU 2546 饭卡
http://acm.hdu.edu.cn/showproblem.php?pid=2546 呆呆. 饭卡 Time Limit: 5000/1000 MS (Java/Others) Memo ...
- 【贪心+背包】【HDU2546】【饭卡】
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- HDU2546:饭卡(01背包)
HDU2546:饭卡 http://acm.hdu.edu.cn/showproblem.php?pid=2546 当我们遇到问题选择物体的价值和顺序相关时就需要,排完序后对其01处理.这题因为当我们 ...
- 饭卡------HDOJ杭电2546(还是01背包!!!!!!)
Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩 ...
随机推荐
- ORM对象关系映射
ORM 总结: ORM:对象关系映射 作用: 1.将定义数据库模型类--> 数据库表 2.将定义数据库模型类中的属性--->数据库表字段 3.将模型对象的操作(add,delete,com ...
- 清除UIWebView缓存
//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...
- git 修改客户端用户名和密码
1.修改某个具体项目中的用户名密码 vim xx/{yourProject dir }/.git/.git-credentials 在.git-credentials文件中配置用户名密码 https: ...
- leetcode85
class Solution { public int maximalRectangle(char[][] matrix) { if(matrix == null || matrix.length = ...
- 基于windows平台搭建elasticsearch
部署准备 elasticsearch-6.0.1.zip--https://www.elastic.co/downloads/elasticsearch elasticsearch-head-mast ...
- C++ 下面的AIDL
转自https://android.googlesource.com/platform/system/tools/aidl/+/brillo-m10-dev/docs/aidl-cpp.md. Bac ...
- Mesh属性[Unity]
Mesh属性[Unity] Mesh是Unity内的一个组件,称为网格组件.3D网格是Unity中最重要的图形元素.在Unity中存在多种组件用于渲染标准网格或者蒙皮网格.拖尾或者3D线条. 在Uni ...
- mysql_day01
1.MySQL概述 1.什么是数据库 数据库是一个存储数据的仓库 2.都有哪些公司在用数据库 金融机构.游戏网站.购物网站.论坛网站 ... ... 3.提供数据库服务的软件 1.软件分类 MySQL ...
- 秦殇 xbm buffer
秦殇的图片是封装在lib文件中的, 而且格式为xbm, xbm具体的结构
- Python基础-python数据类型(四)
python数据类型 在python中,变量就是变量,它没有类型,我们所说的类型是变量所指的内存中对象的类型. python中的数据类型: 1.数字 python中没有专门定义常量的方式,通常使用大写 ...