link:http://acm.hdu.edu.cn/showproblem.php?pid=2546

也算一个贪心的想法吧.

先把总钱数减去5,再把价值最大的挑出来.然后用01背包.最终买下挑出来的那个价值最大的商品.这样的话,我就实现了最终用最少的钱数买了价值最多的商品,剩下钱数当然也是最少了.

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}};
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
int n, V;
int c[], f[];
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
while (cin>>n)
{
if(!n) break;
for (int i = ; i < n; ++i) cin>>c[i];
cin>>V;
V-=;
if (V<)
{
cout<<V+<<endl; continue;
}
int tmp=-, Max;
for (int i = ; i <n; ++i)
if(tmp<c[i]) tmp=c[i], Max=i;
memset(f,,sizeof(f));
for (int i=;i<n;++i)
{
if (i==Max) continue;
for (int v = V; v>=c[i]; --v)
{
f[v] = max(f[v], f[v-c[i]]+c[i]);
}
}
cout<<V+-f[V]-c[Max]<<endl;
}
return ;
}

hdu2546 饭卡    01背包的更多相关文章

  1. HDU2546:饭卡(01背包)

    HDU2546:饭卡 http://acm.hdu.edu.cn/showproblem.php?pid=2546 当我们遇到问题选择物体的价值和顺序相关时就需要,排完序后对其01处理.这题因为当我们 ...

  2. hdu_2546_饭卡(01背包)

    题目连接:hdu_2546_饭卡 题意:中文,不解释 题解:先拿5元来买最贵的,最后就是一个01背包,这里也算用到贪心的思想 #include<bits/stdc++.h> #define ...

  3. HDU 2546 饭卡(01背包裸题)

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  4. HDU 2546 饭卡(01 背包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路:需要首先处理一下的的01背包,当饭卡余额大于等于5时,是什么都能买的,所以题目要饭卡余额最小, ...

  5. HDU -2546饭卡(01背包+贪心)

    这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择 ...

  6. hdu 2546 饭卡 01背包

    先将前n-1个从小到大排序.对m-5进行01背包.然后答案就是m-dp[m-5]-a[n-1] 至于为什么最后减去最贵的菜品,而不是把最贵的菜品也放到01背包里呢, 由于假设能够把最贵菜品a[n-1] ...

  7. hdoj 2546 饭卡(0-1背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路分析:该问题为0-1背包问题的变形题:问题求余额最少,设开始的余额为V,则求得用V-5可以买 ...

  8. HDU 2546 饭卡 01背包变形

    题目大意:中文题就不多说了 题目思路:由题意可知,只要高于5元,就可以随便刷,那我们就把最贵的留在最后刷.但是如果低于5元就什么也不能刷(哪怕你要买的物品价格不足五元),所以我们可以先求出(n-5)元 ...

  9. [HDU2546]饭卡<dp 01背包>

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 #题目描述: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前, ...

  10. HDU2546 饭卡(背包)

    开始写成01背包的形式,求m元可买物品价值的最大值 dp[j] = max(dp[j], dp[j - pri[i]] + pri[i]) 结果为m - dp[m] 但后来发现是有问题的, 比如这组过 ...

随机推荐

  1. zabbix邮件报警

    #!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...

  2. ROS创建工作空间(三)

    查看正在使用的ROS工作空间,使用命令 echo $ROS_PACKAGE_PATH 我新建了两个

  3. TDBGridEh的 搜索面板 TDBGridSearchPanel

    TCustomDBGridEh.Create FSearchPanelControl := TDBGridSearchPanelControlEh.Create(Self); //这里,创建的构造函数 ...

  4. 注册表 ReadBool类型和 ReadInteger 的关系

    function TRegistry.ReadBool(const Name: string): Boolean; begin Result := ReadInteger(Name) <> ...

  5. C# Inject

    using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Di ...

  6. python利用or在列表解析中调用多个函数.py

    python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...

  7. git——学习笔记(一)

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013745374151782e ...

  8. 如何区别PeekMessage&GetMessage SendMessage&PostMessage

    转自http://blog.csdn.net/young0325/article/details/6430664 Peekmessage和Getmessage都是向系统的消息队列中取得消息,不过性质不 ...

  9. UVa 10318 Security Panel

    题意:给你一个3*3的翻转模版,深色部分表示翻转,浅色部分不变.然后你可以在r*c的矩形里依照模版进行翻转,要求所有点亮所有块.输出最小的步骤. 思路:有一点比较好想.每个块至多被翻转一次,翻两次的效 ...

  10. 解决win7资源监视器不能开启

    刚开始时是这样,点击开始监控,无效 需要开始服务即可解决