饭卡(card)

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/31

Description

电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。

某天,食堂中有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

HINT

题意

题解:

01背包,跑容量为m-5,物品为n-1件的01背包。

首先把最大的数挑出来,然后跑01背包

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 200000
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int dp[maxn];
int a[maxn];
int main()
{
//test;
int n,m;
while(cin>>n)
{
memset(dp,,sizeof(dp));
if(n==)
break;
for(int i=;i<=n;i++)
a[i]=read();
sort(a+,a+n+);
m=read();
if(m<)
{
cout<<m<<endl;
continue;
}
for(int i=;i<n;i++)
for(int j=m-;j>=a[i];j--)
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
cout<<m-dp[m-]-a[n]<<endl;
}
}

cdoj 31 饭卡(card) 01背包的更多相关文章

  1. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  2. hdu 2546 饭卡 (01背包)

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  3. hdu 2546 饭卡【01背包】

    题目链接:https://vjudge.net/contest/103424#problem/C 饭卡                                Time Limit: 5000/ ...

  4. HDU 2546:饭卡(01背包)

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  5. 题解报告:hdu 2546 饭卡(01背包)

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  6. 饭卡-HDU2546(01背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory L ...

  7. [HDU2546]饭卡<dp 01背包>

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 #题目描述: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前, ...

  8. HDU 2546 饭卡(0-1背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2546 题意: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金 ...

  9. HDU 2546 饭卡(01背包)

    题目代号:HDU 2546 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/ ...

随机推荐

  1. HDU5808Price List Strike Back (BestCoder Round #86 E) cdq分治+背包

    严格按题解写,看能不能形成sum,只需要分割当前sum怎么由两边组成就好 #include <cstdio> #include <cstring> #include <c ...

  2. 设置UIImage 圆角

    //设置UIImage圆角 @interface UIImage(UIRoundedRectImage) + (id) createRoundedRectImage:(UIImage*)image s ...

  3. Tesseract-OCR识别验证码

    1. 安装Tesseract-OCR,安装后测试下是否安装成功

  4. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 unselect_from_list_by_index(self, locator, *indexes)

    def unselect_from_list_by_index(self, locator, *indexes): """Unselects `*indexes` fro ...

  5. php codeigniter (CI) oracle 数据库配置-宋正河整理

    database.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $active_group = 'default'; $active_record ...

  6. Hadoop 中疑问解析

    Hadoop 中疑问解析 FAQ问题剖析 一.HDFS 文件备份与数据安全性分析1 HDFS 原理分析1.1 Hdfs master/slave模型 hdfs采用的是master/slave模型,一个 ...

  7. android系统的文件夹选择器

    aFileChooser: https://github.com/iPaulPro/aFileChooser/issues, 这个是最适合android的文件选择器,看看有无可能改成文件夹选择器. f ...

  8. js冲突

    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(//"~/Scripts/modernizr-2.6.2 ...

  9. sql中获得时间的参数

    (----我错了,以下非php.php用例:FORM_UNIXTIME($time, '%H')取小时数.---) 返回表示指定日期的指定日期部分的整数. 语法 DATEPART ( datepart ...

  10. 配置cisco路由器特定时间重启

    方法一: Router1#reload in 20 Reload scheduled for 11:33:53 EST Sat Feb 1 2003 (in 20 minutes) Proceed w ...