hdu2126 Buy the souvenirs 01背包变形 dp
来源:互联网 发布:淘宝买家修改评论 编辑:程序博客网 时间:2024/04/20 19:30
链接:点我
题意:n个物品,m元钱,每个物品最多买一次,问最多可以买几件物品,并且输出方案数。加一维表示已经买几件物品。
我这里用了一个数组dp[v][2] ,dp[v][0] 储存原本要记录的 dp ,而 dp[v][1] 则记录方案数目。记住需要初始化,把所有 dp[i][1] 都设置成1。
#include <cstdio>#include <cstring>#include <iostream>using namespace std;#define max(a,b)((a)>(b)?(a):(b))const int maxn = 33;int main(){int t;scanf("%d", &t);while (t--){int n, rmb, cost[maxn], dp[505][2];int i, j;scanf("%d %d", &n, &rmb);for (i=0; i<n; i++) scanf("%d", &cost[i]);memset(dp, 0, sizeof(dp));for (i=0; i<=rmb; i++) dp[i][1] = 1;for (i=0; i<n; i++){for (j=rmb; j>=cost[i]; j--){if (dp[j][0] == dp[j-cost[i]][0] + 1)dp[j][1] = dp[j-cost[i]][1] + dp[j][1];else 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];}}}if (dp[rmb][0] != 0)printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", dp[rmb][1], dp[rmb][0]);elseprintf("Sorry, you can't buy anything.\n");}return 0;}
RANK 排名居然第一,贴出来纪念一下。。
- hdu2126 Buy the souvenirs 01背包变形 dp
- hdu2126 Buy the souvenirs (01)
- HDU2126:Buy the souvenirs(01)
- hdu2126---Buy the souvenirs(01背包方案数)
- hdu2126 Buy the souvenirs(01背包变化)
- 【01背包方案数】HDU2126-Buy the souvenirs
- hdu2126 Buy the souvenirs(01背包+路径)
- HDU2126:Buy the souvenirs(类01背包)
- HDU2126:Buy the souvenirs
- hdu2126 Buy the souvenirs
- 【HDU2126】【Buy the souvenirs】
- 01背包变形 之 hdu 2126 Buy the souvenirs
- HDU 2126 Buy the souvenirs (dp 二维01背包)
- HDU 2126 Buy the souvenirs(DP:01背包)
- hdu 2126 Buy the souvenirs 01背包+计数dp
- Buy the souvenirs - HDU 2126 背包dp
- hdu2126——Buy the souvenirs——————【01背包记录物品最多时种数】
- HDU 2126 - Buy the souvenirs(01背包)
- 关于android加载网页的一些问题
- poj1639 Picnic Planning
- Qt自制Model模型
- 黑马程序员-对象的初始化过程
- 问题:能用Java读取一个Excel文件吗
- hdu2126 Buy the souvenirs 01背包变形 dp
- java2-基础
- UITableView中的各种小用法
- 题目1203:IP地址
- java flex blazeds 开发相关资料
- UML建模语言7种图(以银行ATM系统为例)
- UITextView 退出键盘的方式
- Python 中的 exec 和 eval
- WPF打开文件对话框和保存文件对话框