您的位置:首页 > 其它

HDU 2126 Buy the souvenirs

2015-04-11 18:39 369 查看

Buy the souvenirs

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2126
64-bit integer IO format: %I64d Java class name: Main

When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will buy some ones with pleasure. Not only can they give the souvenirs to their friends and families as gifts, but also can the souvenirs leave them good recollections. All in all, the prices of souvenirs are not very dear, and the souvenirs are also very lovable and interesting. But the money the people have is under the control. They can’t buy a lot, but only a few. So after they admire all the souvenirs, they decide to buy some ones, and they have many combinations to select, but there are no two ones with the same kind in any combination. Now there is a blank written by the names and prices of the souvenirs, as a top coder all around the world, you should calculate how many selections you have, and any selection owns the most kinds of different souvenirs. For instance:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int dp[maxn][2],cost[35],T,n,m;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
for(int i = 0; i < n; ++i)
scanf("%d",cost+i);
memset(dp,0,sizeof dp);
for(int i = 0; i <= m; ++i) dp[i][1] = 1;
for(int i = 0; i < n; ++i){
for(int j = m; j >= cost[i]; --j)
if(dp[j][0] < dp[j-cost[i]][0] + 1){
dp[j][0] = dp[j-cost[i]][0] + 1;
dp[j][1] = dp[j-cost[i]][1];
}else if(dp[j][0] == dp[j-cost[i]][0] + 1)
dp[j][1] += dp[j - cost[i]][1];
}
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]);
else puts("Sorry, you can't buy anything.");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: