21 篇文章 0 订阅
13 篇文章 0 订阅
89 篇文章 0 订阅

# 小D和他的魔法石

——>点我跳转<——

## 代码

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long

using namespace std;

int n, m, k, a[1001], b[1001], times;

int main() {
scanf("%d %d %d", &n, &m, &k);

if (k == 0) {//不能变换，那就是一道普通的背包
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) scanf("%d", &b[i]);

ll f[2][1001], ans = 0;
memset(f, 0, sizeof(f));
for (int i = 1; i <= n; i++)
for (int j = m; j >= 0; j--) {
f[i & 1][j] = f[(i - 1) & 1][j];
times = 1;
while (j - a[i] * times >= 0) {
f[i & 1][j] = max(f[i & 1][j], f[(i - 1) & 1][j - a[i] * times] + b[i] * times);
times++;
}
}

for (int i = 0; i <= m; i++)
ans = max(ans, f[n & 1][i]);

printf("%lld", ans);

return 0;
}

if (n == 2) {//只有两种魔法石
int maxn = 0, minn = 2147483647;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
minn = min(minn, a[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
maxn = max(maxn, b[i]);
}
if ((a[1] == minn && b[1] != maxn) || (a[1] != minn && b[1] == maxn)) {//原本不是最优
if (k & 1) {//能在用完交换次数后达到我们要的最优状态
printf("%lld", 1ll * maxn * (1ll * (m / minn)));

return 0;
}
else {//不能，那就是背包
ll f[2][1001], ans = 0;
memset(f, 0, sizeof(f));
for (int i = 1; i <= n; i++)
for (int j = m; j >= 0; j--) {
f[i & 1][j] = f[(i - 1) & 1][j];
times = 1;
while (j - a[i] * times >= 0) {
f[i & 1][j] = max(f[i & 1][j], f[(i - 1) & 1][j - a[i] * times] + b[i] * times);
times++;
}
}

for (int i = 0; i <= m; i++)
ans = max(ans, f[n & 1][i]);

printf("%lld", ans);

return 0;
}
}
else {//原本是最优
if (k & 1) {//不能
swap(a[1], a[2]);
ll f[2][1001], ans = 0;
memset(f, 0, sizeof(f));
for (int i = 1; i <= n; i++)
for (int j = m; j >= 0; j--) {
f[i & 1][j] = f[(i - 1) & 1][j];
times = 1;
while (j - a[i] * times >= 0) {
f[i & 1][j] = max(f[i & 1][j], f[(i - 1) & 1][j - a[i] * times] + b[i] * times);
times++;
}
}

for (int i = 0; i <= m; i++)
ans = max(ans, f[n & 1][i]);

printf("%lld", ans);

return 0;
}
else {//能
printf("%lld", 1ll * maxn * (1ll * (m / minn)));

return 0;
}
}

return 0;
}

int maxn = 0, minn = 2147483647;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
minn = min(minn, a[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
maxn = max(maxn, b[i]);
}

printf("%lld", 1ll * maxn * (1ll * (m / minn)));

return 0;
}

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

04-21 143
05-13 185
01-06 36
02-07 2448
10-18 2万+
10-18 902
10-21 370
10-18 292
10-25 248
10-25 213
10-28 193
10-22 139