zoukankan      html  css  js  c++  java
  • HDU 2546 饭卡【贪心+01背包】

    饭卡

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 39562    Accepted Submission(s): 13548


    Problem 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
     
    Source

    【分析】:当钱数少于5块钱时,不能购买东西。

    根据题意可以知道只要饭卡大于等于5元,我们就可以买任何的菜,即使钱不够也没关系,想要余额最小,一定要选最贵的。提前需要一个预处理,排序后找到价格最高的饭菜,花费5元买去它。

    然后就变成了n-1个物品, m-5元钱的背包问题。

    【代码】:

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,n,x) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 1e5 + 20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int n,m;
    int v[maxn], w[maxn],dp[maxn];
    int t;
    /*
    那么最大的菜价一定是最后要减的,那么我们将这一组饭菜价格按从小到大排序,将最大的那个先放一边,我们接下来就是要把剩下的一些菜价用我们手头的余额减,当然必须要保证减去的金额小于等于sum-5,这样我们才能在最后一次把最大的菜价刷掉。
    */
    int main()
    {
        while(cin>>n,n){
    
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=n;i++)
                cin>>w[i];
            sort(w+1,w+n+1);
            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--){
                     dp[j]=max(dp[j],dp[j-w[i]]+w[i]);
                }
            }
            cout<<m-w[n]-dp[m-5]<<endl;
        }
    }

     

  • 相关阅读:
    Python 第四十九章 css 补充
    Python 第四十八章 css样式
    Python 第四十七章 css介绍
    iOS 流媒体 基本使用 和方法注意
    iOS 展示 gif
    iOS UIImage UIImageView 展示图片 不变形 处理
    UIImageView 获取图片的 宽 高
    iOS tabbar 上面更换任意图
    iOS 屏幕原点坐标 && 导航栏风格的自定义
    iOS NSDateFormatter 不安全线程 处理
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9046049.html
Copyright © 2011-2022 走看看