标签:eof input namespace 表示 efi ane ring pre while
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30705 Accepted Submission(s): 10515
思路:
首先拿五元买最贵的东西,接下来背包的容量就为剩下的m-5,物品数量为n-1的背包问题。
状态转移方程为:dp[j] = max(dp[j],dp[j-num[i]]+num[i]]);
,dp[j]表示买前i件物品,预算为j时的最大花销
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 1005 using namespace std; int num[maxn], dp[maxn]; int max(int a, int b){ if(a>b) return a; return b; } int main() { int n, m; while(scanf("%d", &n), n){ memset(num,0,sizeof(num)); for(int i = 1; i <= n; i ++) scanf("%d", &num[i]); sort(num+1,num+n+1); //注意你要排序的是n,所以你前面加了1,后面也要改为+n+1 scanf("%d", &m); if(m < 5){ printf("%d\n", m); continue; } m -= 5; for(int i = 0; i <= m; i ++) dp[i] = 0; for(int i = 1; i < n; i ++){ for(int j = m; j >= num[i]; j --){ dp[j] = max(dp[j], dp[j - num[i]] + num[i]); //一次次从dp[m]更新到dp[num[i]] } } printf("%d\n", m + 5 - num[n] - dp[m]); } return 0; }
标签:eof input namespace 表示 efi ane ring pre while
原文地址:http://www.cnblogs.com/l609929321/p/7227461.html