hdu 2546 饭卡-动态规划(基础中的基础题)
来源:互联网 发布:html的js加减怎么做 编辑:程序博客网 时间:2024/05/21 12:50
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。
Input:
多组数据。对于每组数据:第一行为正整数n,表示菜的数量。n<=1000。第二行包括n个正整数,表示每种菜的价格。价格不超过50。第三行包括一个正整数m,表示卡上的余额。m<=1000。n=0表示数据结束。
Output:
对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。
Sample Input:
1505101 2 3 2 1 1 2 3 2 1500
Sample Output:
-4532
思路:
没啥可说的,直接代码:
#include<stdio.h>#include<iostream>#include<cstring>#include<cmath>#include<algorithm>using namespace std;int board[1005];int dp[1005];int main(){int n;while(scanf("%d",&n)&&n){memset(dp,0,sizeof(dp));for(int i=0 ; i<n ; i++)scanf("%d",&board[i]);sort(board,board+n);int yue; //yue是余额 scanf("%d",&yue);if(yue<5){printf("%d\n",yue);continue;}yue -= 5;for(int i=0 ; i<n-1 ; i++){for(int j=yue ; j>=board[i] ; j--){dp[j] = max(dp[j],dp[j-board[i]]+board[i]);}}printf("%d\n",yue+5-dp[yue]-board[n-1]);}return 0;}
阅读全文
0 0
- hdu 2546 饭卡-动态规划(基础中的基础题)
- 动态规划基础题 HDU 1068
- 【动态规划(一)】动态规划基础
- HDU 1058 Humble Numbers—dp动态规划基础题
- 动态规划 -- 动态规划基础
- 动态规划基础
- 动态规划基础
- 算法基础---动态规划
- 动态规划 基础总结
- 动态规划入门,基础
- 动态规划基础讲
- 动态规划基础
- 动态规划基础
- 【专题】基础动态规划
- 基础动态规划
- 算法基础--动态规划
- LeetCode基础--动态规划
- HDU 1565 方格取数(1) HDU 2167 Pebbles 基础状态压缩动态规划
- 八大排序算法总结(超详细)
- 旅游(国家集训队)
- 一个银行账户5000块,两夫妻一个拿着 存折,一个拿着卡,开始取钱比赛,每次只能取一千块,要求不准出现线程安全问题。
- HDU 2048 数塔 (DP)
- HDU 2602 Bone Collector(01背包裸题)
- hdu 2546 饭卡-动态规划(基础中的基础题)
- 赌博-值得玩耍的棋牌 3
- git常见问题之git pull时Please specify which branch you want to merge with.
- FTP 主动模式与被动模式传输区别
- git常见问题之git pull origin master时fatal: refusing to merge unrelated histories
- 常用几个交换机命令
- 利用多线程写一个卖票程序
- fastjson 阿里巴巴json使用
- git常见问题之git pull时Automatic merge failed; fix conflicts and then commit the result.