/*
从一组数据中选出n个数,使这n个数的和最接近一个值x,
背包问题,
从一系列菜中,从最贵的菜(MAX)之外中选出几个菜,使菜的总价格sum最接近money-5;money-sum-MAX;
钱数相当于背包总容量,菜相当于价值和体积一样物品;
*/
 
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[1050];
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        int Greens[n];
        memset(dp,0,sizeof(dp));
        memset(Greens,0,sizeof(Greens));
        int max1=-0x3f3f3f3f;
        for(int i=0; i<n; i++)
            scanf("%d",&Greens[i]);
        int money;
        scanf("%d",&money);
        if(money<5)
        {
            printf("%d\n",money);
            continue;
        }
        sort(Greens,Greens+n);
        for(int i=0;i<n-1;i++) //遍历每道菜
            for(int j=money-5;j>=Greens[i];j--) //判断能不能买这种菜
            dp[j]=max(dp[j],dp[j-Greens[i]]+Greens[i]);
            int MAX=dp[money-5]; //最接近money-5的值
        printf("%d\n",money-MAX-Greens[n-1]); //买过可以买的菜之后剩的钱数再买最贵的菜即可以使剩余钱数最少
    }
    return 0;
}

饭卡 (背包01 一维数组) http://acm.hdu.edu.cn/showproblem.php?pid=2546的更多相关文章

  1. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  2. KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...

  3. HDU-4632 http://acm.hdu.edu.cn/showproblem.php?pid=4632

    http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意: 一个字符串,有多少个subsequence是回文串. 别人的题解: 用dp[i][j]表示这一段里 ...

  4. 待补 http://acm.hdu.edu.cn/showproblem.php?pid=6602

    http://acm.hdu.edu.cn/showproblem.php?pid=6602 终于能够看懂的题解: https://blog.csdn.net/qq_40871466/article/ ...

  5. http://acm.hdu.edu.cn/showproblem.php?pid=2579

    #include<stdio.h> #include<string.h> #include<queue> #define N 110 int m, n, k, x1 ...

  6. Problem - 1062 http://acm.hdu.edu.cn/showproblem.php?pid=1062

    对输入字符串的字符的倒置,在这个程序中,我觉得自己最大的问题是怎么识别一个字符,代码中有t个字符串,每个字符串,每个字符串中有若干个单词,单词之间有空格,所以对于下列的正确答案,我的疑惑是当我键盘输入 ...

  7. HDU-1257 导弹拦截系统 http://acm.hdu.edu.cn/showproblem.php?pid=1257

    Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...

  8. KMP应用http://acm.hdu.edu.cn/showproblem.php?pid=2594

    riemann与marjorie拼接后riemannmarjorie前缀与后缀公共部分为 rie 长度为 3(即next[l] = next[14]的值,l为拼接后的长度)但:aaaa与aa拼接后aa ...

  9. HDU 2544 最短路 http://acm.hdu.edu.cn/showproblem.php?pid=2544

    //代码: //方法1:Dijkstra's Algorithm #include<stdio.h> #include<math.h> #include<string.h ...

随机推荐

  1. 传统的log4j实战

    /** * */ package log4j; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator ...

  2. 【转】Windows环境下.NET 操作Oracle问题

    目前,Windows操作系统可以分成两类,32位和64位(64位也区分x86_64位和Itanium ),同时Oracle客户端也做了同样的区分. 在安装和开发的过程中,经常会遇到一些问题,本文就总结 ...

  3. 在iframe中获取iframe外的对象

    parent.document.getElementById("dom ID"); $($(parent.document.getElementById("video-i ...

  4. Objective - C 中NSString (字符串)与C中的字符串转换问题

    NSString是一个常用的类,NSString是原生支持unicode C中的字符串 比如char * a = "hello world";  是utf8类型的, char* d ...

  5. 2014年10月16号--for语句实例

    Console.WriteLine("一对小兔一个月之后长成大兔,再过一个月后生新的一对兔子,且两年之后有多少对兔子,就是三兔子幼兔,小兔,成兔"); Console.WriteL ...

  6. PS2251-07 海力士(金士顿U盘量产,成功!)

    U盘挂掉了,用芯片无忧测到是这个样子的,看到是PS2251-07 海力士的 网上找了很多方法都不成功,最后找到了两个可以量产成功的方法,建议使用第一种. 首先,附上三个检测工具 芯片无忧.GetInf ...

  7. [LeetCode OJ]-Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. 【HDU3802】【降幂大法+矩阵加速+特征方程】Ipad,IPhone

    Problem Description In ACM_DIY, there is one master called “Lost”. As we know he is a “-2Dai”, which ...

  9. ICE学习第一步-----配置ICE环境变量

    安装 ICE: 1.下载ICE: http://www.zeroc.com/download.html 下载说明:ICE支持语言(C++, Java, C#, Visual Basic,Python, ...

  10. Spring 整合Redis 出现 afterPropertiesSet signature: ()V) Incompatible argument to function 解决办法

    正在做SpringMVC+Redis整合的练习 使用的是 spring-data-redis 和 Jedis 配置好之后出现了以下错误: Caused by: java.lang.VerifyErro ...