HDU 2546(01背包)
生活随笔
收集整理的這篇文章主要介紹了
HDU 2546(01背包)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
飯卡
Time Limit: 5000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16453????Accepted Submission(s): 5721
某天,食堂中有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 UESTC 6th Programming Contest Online?裸的01背包,暑假集訓的時候竟然不會了。。。重做01背包吧,唉。:-(~~~~~~~
#include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; #define ll long long #define _cle(m, a) memset(m, a, sizeof(m)) #define repu(i, a, b) for(int i = a; i < b; i++) #define repd(i, a, b) for(int i = b; i >= a; i--) #define sfi(n) scanf("%d", &n) #define sfl(n) scanf("%I64d", &n) #define pfi(n) printf("%d\n", n) #define pfl(n) printf("%I64d\n", n) #define MAXN 1105 int dp[MAXN]; int v[MAXN]; int main() {int n, m;while(sfi(n), n){repu(i, 0, n) sfi(v[i]);sfi(m);if(m < 5) { pfi(m); continue; }sort(v, v + n);_cle(dp, 0);for(int i = 0; i < n - 1; i++)for(int j = m - 5; j >= v[i]; j--){dp[j] = max(dp[j], dp[j - v[i]] + v[i]);}pfi(m - v[n - 1] - dp[m - 5]);}return 0; } View Code?
轉載于:https://www.cnblogs.com/sunus/p/4726422.html
總結
以上是生活随笔為你收集整理的HDU 2546(01背包)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lt;%%gt;创建内联代码块(表达)
- 下一篇: Activiti 接收任务活动