POJ 1742 hdu 2844 Coins

时间:2022-10-15 16:01:47

题目链接http://poj.org/problem?id=1742

http://acm.hdu.edu.cn/showproblem.php?pid=2844

题目分类:动态规划

代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm> using namespace std;
int n,m;
bool flag[]; struct P
{
int a,b; }point[]; int cmp(P X,P Y)
{
return X.a<Y.a;
} int main()
{
while(scanf("%d %d",&n,&m)&&(n+m))
{
memset(point,,sizeof(point));
memset(flag,,sizeof(flag)); for(int i=;i<=n;i++)
{
scanf("%d",&point[i].a);
}
for(int i=;i<=n;i++)
{
scanf("%d",&point[i].b);
}
sort(point+,point+n+,cmp);
int sum[]; flag[]=;
for(int i=;i<=n;i++)
{
memset(sum,,sizeof(sum));
for(int k=point[i].a;k<=m;k++)
{
if(flag[k-point[i].a]&&sum[k-point[i].a]<point[i].b&&!flag[k])
{
sum[k] = sum[k-point[i].a] + ;
flag[k] = ;
}
}
} int ans=;
for(int i=;i<=m;i++)
{
if(flag[i])
{
//printf("sss== %d\n",i);
ans++;
}
}
printf("%d\n",ans);
}
return ;
}
#include<bits/stdc++.h>

using namespace std;

bool dp[];
int TmpVal[];
int val[]; int main()
{
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int n, m;
while(cin>>n>>m)
{
if(n== && m==)
break;
memset(dp, , sizeof(dp));
for(int i=;i<=n;i++)
cin>>TmpVal[i];
int N = ;
for(int i=;i<=n;i++)
{
int num;
cin>>num;
int j = 1;
while(j<=num)
{
val[++N] = TmpVal[i] * j;
num -= j;
j = (j<<1);
}
if(num)
{
val[++N] = TmpVal[i] *
num;
}

}
dp[0] = 1;
for(int i=;i<=N;i++)
{
for(int j=m;j>=val[i];j--)
{
if(dp[j-val[i]])
dp[j] = ;
}
}
int ans = ;
for(int i=;i<=m;i++)
if(dp[i])
ans++;
printf("%d\n", ans);
}
return ;
}