题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546

先找出最贵的那个菜,这个菜一定是最后买的那个。然后再在前n-1个菜里做01背包。找出不超过m-5的最大价值。然后用m减去这两个值即可。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; //kirai
const int maxn = ;
int n, m;
int dp[maxn];
int w[maxn]; int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n) && n) {
int maxx = -;
int pos;
for(int i = ; i <= n; i++) {
scanf("%d", &w[i]);
if(maxx < w[i]) {
maxx = w[i];
pos = i;
}
}
swap(w[pos], w[n]);
scanf("%d", &m);
if(m < ) {
printf("%d\n", m);
continue;
}
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++) {
for(int j = m - ; j >= w[i]; j--) {
dp[j] = max(dp[j], dp[j-w[i]]+w[i]);
}
}
printf("%d\n", m - maxx - dp[m-]);
}
return ;
}

[HDOJ2546] 饭卡 (01背包)的更多相关文章

  1. hdu_2546_饭卡(01背包)

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

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

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

  3. HDU 2546 饭卡(01 背包)

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

  4. HDU2546:饭卡(01背包)

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

  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. hdu 2546 饭卡(背包)

      设饭卡余额为total 此题经分析 可以得出:要求选出一些饭菜 时消费量尽量接近total-5元 然后再买一个饭菜 以达到透支... 可以证明 最后买的那个饭菜是饭菜中价值最大的. 证明 设a1 ...

随机推荐

  1. Win32 Plus Extra Height of Caption Bar

    you set the size of the non-client area by handling the WM_NCCALCSIZE message. But don't do this unl ...

  2. Codeforces Round #204 (Div. 2)->D. Jeff and Furik

    D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. Unity使用外部版本控制SVN

    原地址:http://www.cnblogs.com/realtimepixels/p/3652146.html Using External Version Control Systems with ...

  4. (转)Linux进程间通信

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 谢谢nonoob纠错 我们在Linux信号基础中已经说明,信号可以看作一种粗糙的进 ...

  5. AIZU 2251

    Merry Christmas Time Limit : 8 sec, Memory Limit : 65536 KB Problem J: Merry Christmas International ...

  6. 历代诗词咏宁夏注释1----常星景:< 六盘>

    六盘 常星景 关中形势甲天下,四岳分峙西太华.[1] 中有汭泾经纬之,六盘嵚崎历历落.[2] □□□□其流亚,终年峰头雪不消. 弟畜太白兒美高,眼底培缕纷纷何足数,呼吸想通天尺五.[3] 西北堆镇一切 ...

  7. 为jquery qrcode生成的二维码嵌入图片

    在一次微信项目中,需要实现通过扫描二维码来进行会议签到,二维码的生成选择了qrcode.js的版本,然后使用jquery.qrcode.js插件来绘制二维码. <script type=&quo ...

  8. Codeforces Round #258 (Div. 2)(A,B,C,D)

    题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...

  9. (转)【移动开发】Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewFlow)

    转自: http://smallwoniu.blog.51cto.com/3911954/1308959 现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习 ...

  10. Linux系统新手学习的11点建议

    随着Linux应用的扩展许多朋友开始接触Linux,根据学习Windwos的经验往往有一些茫然的感觉:不知从何处开始学起.这里介绍学习Linux的一些建议. 一.从基础开始:常常有些朋友在Linux论 ...