标签:
2 4 7 1 2 3 4 4 0 1 2 3 4
You have 2 selection(s) to buy with 3 kind(s) of souvenirs. Sorry, you can‘t buy anything.
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 505; 4 int dp[maxn][2],cost[35],T,n,m; 5 int main(){ 6 scanf("%d",&T); 7 while(T--){ 8 scanf("%d %d",&n,&m); 9 for(int i = 0; i < n; ++i) 10 scanf("%d",cost+i); 11 memset(dp,0,sizeof dp); 12 for(int i = 0; i <= m; ++i) dp[i][1] = 1; 13 for(int i = 0; i < n; ++i){ 14 for(int j = m; j >= cost[i]; --j) 15 if(dp[j][0] < dp[j-cost[i]][0] + 1){ 16 dp[j][0] = dp[j-cost[i]][0] + 1; 17 dp[j][1] = dp[j-cost[i]][1]; 18 }else if(dp[j][0] == dp[j-cost[i]][0] + 1) 19 dp[j][1] += dp[j - cost[i]][1]; 20 } 21 if(dp[m][0]) printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[m][1],dp[m][0]); 22 else puts("Sorry, you can‘t buy anything."); 23 } 24 return 0; 25 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4418182.html