您的位置:首页 > 其它

【HDOJ】2844 Coins

2014-05-26 21:38 204 查看
完全背包。

#include <stdio.h>
#include <string.h>

int a[105], c[105];
int n, m;
int dp[100005];

int mymax(int a, int b) {
return a>b ? a:b;
}

void CompletePack(int c) {
int i;

for (i=c; i<=m; ++i)
dp[i] = mymax(dp[i], dp[i-c]);
}

void ZeroOnePack(int c) {
int i;

for (i=m; i>=c; --i)
dp[i] = mymax(dp[i], dp[i-c]);
}

void multipack(int c, int n) {
int k;
if (c*n >= m) {
CompletePack(c);
return ;
}
k = 1;
while (k < n) {
ZeroOnePack(k*c);
n -= k;
k *= 2;
}
ZeroOnePack(n*c);
}

int main() {
int i, total;

while (scanf("%d%d",&n,&m)!=EOF && (n||m)) {
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (i=1; i<=n; ++i)
scanf("%d", &a[i]);
for (i=1; i<=n; ++i)
scanf("%d", &c[i]);
for (i=1; i<=n; ++i)
multipack(a[i], c[i]);
total = 0;
for (i=1; i<=m; ++i)
if (dp[i])
++total;
printf("%d\n", total);
}

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