zoukankan      html  css  js  c++  java
  • 饭卡

    饭卡
    Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 16499 Accepted Submission(s): 5742

    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
    UESTC 6th Programming Contest Online
    01背包

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <queue>
    #include <algorithm>
    #define LL long long
    using namespace std;
    
    const int MAX =  110000;
    
    int a[1100];
    
    int dp[1100];
    
    int main()
    {
        int n,m;
        while(scanf("%d",&n)&&n)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
            }
            scanf("%d",&m);
            if(m<5)
            {
                printf("%d
    ",m);
                continue;
            }
            sort(a,a+n);
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n-1;i++)
            {
                for(int j=m;j>=a[i]+5;j--)
                {
                    dp[j]=max(dp[j-a[i]]+a[i],dp[j]);
                }
            }
            printf("%d
    ",m-(dp[m]+a[n-1]));
        }
        return 0;
    }
    
  • 相关阅读:
    求列表中指定元素的位置
    Hash_P1026毒药?解药?
    Hash_集合
    bzoj1483: [HNOI2009]梦幻布丁
    bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板
    容斥原理
    bzoj1042: [HAOI2008]硬币购物
    [Noi2016十连测第五场]二进制的世界
    NOI2016模拟赛Zbox loves stack
    bzoj2038: [2009国家集训队]小Z的袜子(hose)
  • 原文地址:https://www.cnblogs.com/juechen/p/5255988.html
Copyright © 2011-2022 走看看