hdoj 2546 饭卡
饭卡
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15507 Accepted Submission(s):
5358
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。
第一行为正整数n,表示菜的数量。n<=1000。
第二行包括n个正整数,表示每种菜的价格。价格不超过50。
第三行包括一个正整数m,表示卡上的余额。m<=1000。
n=0表示数据结束。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 1010
#define max(x,y)(x>y?x:y)
using namespace std;
bool cmp(int a,int b)
{
return a<b;
}
int s[MAX],dp[MAX];
int main()
{
int n,m,j,i,k,t,sum;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%d",&s[i]);
scanf("%d",&m);
if(m<5)
{
printf("%d\n",m);
continue;
}
memset(dp,0,sizeof(dp));
sum=m-5;
sort(s,s+n);
for(i=0;i<n-1;i++)
{
for(j=sum;j>=s[i];j--)
{
dp[j]=max(dp[j],dp[j-s[i]]+s[i]);
}
}
printf("%d\n",m-dp[sum]-s[n-1]);
}
return 0;
}
hdoj 2546 饭卡的更多相关文章
- HDOJ 2546饭卡(01背包问题)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如 ...
- hdoj 2546 饭卡(0-1背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路分析:该问题为0-1背包问题的变形题:问题求余额最少,设开始的余额为V,则求得用V-5可以买 ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDU 2546 饭卡(01背包)
题目代号:HDU 2546 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/ ...
- HDU 2546 饭卡
http://acm.hdu.edu.cn/showproblem.php?pid=2546 呆呆. 饭卡 Time Limit: 5000/1000 MS (Java/Others) Memo ...
- HDU 2546 饭卡(01背包裸题)
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- hdu 2546 饭卡(背包)
设饭卡余额为total 此题经分析 可以得出:要求选出一些饭菜 时消费量尽量接近total-5元 然后再买一个饭菜 以达到透支... 可以证明 最后买的那个饭菜是饭菜中价值最大的. 证明 设a1 ...
- HDU 2546 饭卡(0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 题意: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金 ...
- HDU 2546 饭卡(01 背包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路:需要首先处理一下的的01背包,当饭卡余额大于等于5时,是什么都能买的,所以题目要饭卡余额最小, ...
随机推荐
- hdu 1560 DNA sequence(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others) ...
- 更改nginx网站根目录
默认网站根目录为/usr/local/nginx/html,要将它改成/homw/www vi /usr/local/nginx/conf/nginx.conf 将其中的 loca ...
- Fckeditor漏洞利用总结
查看编辑器版本FCKeditor/_whatsnew.html——————————————————————————————————————————————————————— —————— 2. Ver ...
- 查看jdk的位数
public class Test { public static void main(String[] args) { System.out.println("bit of JVM is ...
- 让动画不再僵硬:Facebook Rebound Android动画库介绍
introduction official site:http://facebook.github.io/reboundgithub : https://github.com/facebook/reb ...
- int指令理解
以下是王爽老师的<汇编语言>中第十五章中的一段程序代码,其功能是增加9号中断的功能,当按下Esc键时屏幕中显示的字母改变颜色 assume cs:codesg,ss:stack,ds:da ...
- web storm使用和配置
官网:http://www.jetbrains.com/webstorm/ webStorm,File=>setting=>JavaScript-Libraries How WebStor ...
- LINUX中的虚拟文件系统结构
我的博客:www.while0.com 以下以2.6.32版本的内核源码为例: 虚拟文件系统与具体文件系统之间是几组操作函数的对应,包括file_operations,dentry_operation ...
- 《C#并行编程高级教程》第4章 并发集合 笔记
这一章主要介绍了System.Collections.Concurrent下的几个类. ConcurrentQueue<T> 并发队列.完全无锁,使用CAS(compare-and-swa ...
- 从头开始编写一个Orchard网上商店模块(3) - 创建Orchard.Webshop模块项目
原文地址:http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-par ...