HDU 2126
来源:互联网 发布:163邮箱 ssl smtp端口 编辑:程序博客网 时间:2024/05/17 01:25
如有错误,欢迎大神指出!
这又是一道变种的01背包问题,可以加多一维数组代表方案。
解释在代码里面,接下来ac code:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <cmath>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>using namespace std;#define si1(a) scanf("%d",&a)#define si2(a,b) scanf("%d%d",&a,&b)#define sd1(a) scanf("%lf",&a)#define sd2(a,b) scanf("%lf%lf",&a,&b)#define ss1(s) scanf("%s",s)#define pi1(a) printf("%d\n",a)#define pi2(a,b) printf("%d %d\n",a,b)#define mset(a,b) memset(a,b,sizeof(a))#define forb(i,a,b) for(int i=a;i<b;i++)#define ford(i,a,b) for(int i=a;i<=b;i++)typedef long long LL;const int N=1100001;const int M=6666666;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);const double eps=1e-7;int a[35],dp[505][2],n,m;int main(){ int t; si1(t); while(t--) { si2(n,m); mset(dp,0); forb(i,0,n) si1(a[i]); forb(i,0,m) dp[i][1]=1;//开始方案为1,dp【i】【0】代表购买数量,dp【i】【1<span style="font-family: Arial, Helvetica, sans-serif;">】代表购买方案数量</span>
forb(i,0,n) { for(int j=m;j>=a[i];j--) { if(dp[j][0]==dp[j-a[i]][0]+1)//如果相同则代表不同方案; dp[j][1]+=dp[j-a[i]][1];//将方案累加; else if(dp[j][0]<dp[j-a[i]][0]+1)//不同方案,并且可以购买 { dp[j][0]=dp[j-a[i]][0]+1;//购买数量加一 dp[j][1]=dp[j-a[i]][1];//方案传递 } } //printf("%d\n",dp[m-1]); } if(dp[m][0]!=0) printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[m][1],dp[m][0]); else printf("Sorry, you can't buy anything.\n"); } return 0;}
0 0
- HDU 2126
- hdu 2126 DP
- hdu 2126 DP
- hdu 2126(dp)
- hdu 2126背包问题
- hdu 2126 背包种类
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- ok6410 3.0.1内核调用V4L接口出错,解决方法
- 被relativeLayout的grivate center 折腾死了
- 黄阶低级 - 《 Java 核心》- 3.8 控制流程 - 修改0次
- 归并排序
- 查看cloudrea的许可证---可用期限
- HDU 2126
- docker下tomcat环境搭建
- 一些东西+一些小错误。
- poj 2187 Beauty Contest (计算几何)
- 2016.08.14【初中部 NOIP提高组 】模拟赛C题解
- Wcf好教程
- makedown语言中中文输入 首行缩进-博客必备
- linux下MySQL安装及设置
- 如何解读Caffe源码