cdoj31-饭卡(card) (01背包)
http://acm.uestc.edu.cn/#/problem/show/31
饭卡(card)
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。
Input
多组数据。对于每组数据:
- 第一行为正整数n,表示菜的数量。n≤1000。
- 第二行包括n个正整数,表示每种菜的价格。价格不超过50。
- 第三行包括一个正整数m,表示卡上的余额。m≤1000。
n=0表示数据结束。
Output
对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。
Sample input and output
| Sample Input | Sample Output |
|---|---|
1 |
-45 |
题解:01背包。单独取出序列中最大值,然后对剩下的数进行01背包,V为m-5,求得maxValue,最后与序列最大值做减法。
代码:
#include <fstream>
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std; const int N=;
int n,v;
int a[N],dp[N]; int main()
{
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
while(scanf("%d",&n),n){
int m0=,i0=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
if(m0<a[i]) m0=a[i],i0=i;
}
scanf("%d",&v);
if(v<){
printf("%d\n",v);
continue;
}
v-=;
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
if(i==i0) continue;
for(int j=v;j>=a[i];j--)
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
}
printf("%d\n",v+-dp[v]-m0);
}
return ;
}
cdoj31-饭卡(card) (01背包)的更多相关文章
- cdoj 31 饭卡(card) 01背包
饭卡(card) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/31 Des ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- hdu 2546 饭卡 (01背包)
Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...
- hdu 2546 饭卡【01背包】
题目链接:https://vjudge.net/contest/103424#problem/C 饭卡 Time Limit: 5000/ ...
- HDU 2546:饭卡(01背包)
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- 题解报告:hdu 2546 饭卡(01背包)
Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...
- 饭卡-HDU2546(01背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory L ...
- [HDU2546]饭卡<dp 01背包>
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 #题目描述: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前, ...
- HDU 2546 饭卡(0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 题意: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金 ...
- HDU 2546 饭卡(01背包)
题目代号:HDU 2546 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/ ...
随机推荐
- vue插件vue-infinite-loading的使用
vue-infinite-loading官网:https://peachscript.github.io/vue-infinite-loading/ 安装: npm install vue-infin ...
- LuaJavaBridge
http://www.360doc.com/content/14/0117/13/9200790_345940368.shtml quick目录结构介绍 http://cn.cocos2d-x.org ...
- Arrays--codility
lesson 2: Arrays exercise: Problem: Given array A consisting of N integers, return the reversed arra ...
- Linux软连接和硬链接(转)
1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...
- LNMP.ORG 安装LNMP
参考:http://lnmp.org/install.html cd /usr/local/src wget -c http://soft.vpser.net/lnmp/lnmp1.3-full.ta ...
- 初探babel转换器的安装与使用
一.配置.babelrc文件(没有名字的文件) Babel的配置文件是.babelrc,存放在项目的根目录下.使用Babel的第一步,就是配置这个文件. 基本格式如下: { "presets ...
- 软RAID 0的技术概要及实现
1 什么是RAID,RAID的级别和特点 : 什么是RAID呢?全称是 “A Case for Redundant Arrays of Inexpensive Disks (RAID)”,在1987年 ...
- nginx和php-fpm通信的两种方式 unix socket和TCP
nginx和fastcgi的通信方式有两种,一种是TCP 一种是unix socket TCP使用的是 127.0.0.1:9000端口,将fastcgi_pass参数修改为127.0.0.1:900 ...
- java Annotation的应用
一.Annotation 示例 Override Annotation @Override public void onCreate(Bundle savedInstanceState); 二.Ann ...
- java封装后引用的例子
封装好的文件: 另外一个文件引用封装文件的方法: 来源: https://www.runoob.com/java/java-encapsulation.html