zoukankan      html  css  js  c++  java
  • cdoj 31 饭卡(card) 01背包

    饭卡(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[20];
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int dp[maxn];
    int a[maxn];
    int main()
    {
        //test;
        int n,m;
        while(cin>>n)
        {
            memset(dp,0,sizeof(dp));
            if(n==0)
                break;
            for(int i=1;i<=n;i++)
                a[i]=read();
            sort(a+1,a+n+1);
            m=read();
            if(m<5)
            {   
                cout<<m<<endl;
                continue;
            }
            for(int i=1;i<n;i++)
                for(int j=m-5;j>=a[i];j--)
                    dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
            cout<<m-dp[m-5]-a[n]<<endl;
        }
    }
  • 相关阅读:
    刚搬过了,请大家多多支持!!
    [导入]搞到了visual studio 2005 professional edtion
    一些常用的正则表达式
    xinxp的隐藏技巧
    今天领准考证了
    推荐一本.net的好书
    今天装上Linux了
    隐藏在Windows XP中的28个秘密武器
    为 Sharepoint 创建可视的 WebPart
    在 NHibernate 中一切必须是 Virtual 的吗?
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4545066.html
Copyright © 2011-2022 走看看