zoukankan      html  css  js  c++  java
  • HDUOJ 2546 饭卡

    饭卡

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2546
    64-bit integer IO format: %I64d      Java class name: Main
     
    电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于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元 用来买最贵的食物。然后用剩余的钱,进行01背包,取能买价值最多的食物价值。总钱-最大价值-最贵食物价格。
     
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 int n,m,p[1010],dp[1010];
    18 int main() {
    19     while(scanf("%d",&n),n) {
    20         memset(dp,0,sizeof(dp));
    21         for(int i = 1; i <= n; i++) scanf("%d",p+i);
    22         scanf("%d",&m);
    23         sort(p+1,p+n+1);
    24         dp[0] = 0;
    25         if(m < 5) {printf("%d
    ",m);continue;}
    26         for(int i = 1; i < n; i++){
    27             for(int j = m-5; j >= p[i]; j--)
    28                 dp[j] = max(dp[j],dp[j-p[i]] + p[i]);
    29         }
    30         printf("%d
    ",m-dp[m-5]-p[n]);
    31     }
    32     return 0;
    33 }
    View Code
  • 相关阅读:
    当简单的计算遇上了大数,其实大数运算也很简单
    揭开源码的神秘面纱,让源码从此无处藏身
    JAVA对象和XML文档、原来他们之间还有这一出
    JAVA反射其实就是那么一回事
    Metatable让我从心认知了Lua(相知篇)
    Github
    常见问题汇总
    文章目录
    前后端分离下使用SignalR
    IdentityServer_0_参考资料
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3948145.html
Copyright © 2011-2022 走看看