题解:B4515 [四川青少年 C++ 算法设计大赛 2025] 圆度
liu12131415 · · 题解
思路
先处理读入。
为满足在
代码:
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
long long x;
cin >> x;
int a = 0, b = 0;
while (x % 2 == 0) x /= 2, a++;
while (x % 3 == 0) x /= 3, b++;
c2[i] = a;
c3[i] = b;
}
接着,我们进入主要的背包 DP 。
我们知道,选取
容量要设多少呢?据本人
DP 转移式:
(这里的
代码:
memset(f, -0x3f, sizeof(f));
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
int a = c2[i], b = c3[i];
for (int j = k; j >= 1; --j) {
for (int c = C - 1; c >= a; --c) {//这里C多大自己写,但要>1000
if (f[j - 1][c - a] >= 0)
f[j][c] = max(f[j][c], f[j - 1][c - a] + b);
}
}
}
(注:
最后给
注意
::::success[AC code]
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
const int K = 105;
const int C = 1005;//爱开多大开多大,>1000就够了
int f[K][C];
int c2[N], c3[N];
int n, k;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
long long x;
cin >> x;
int a = 0, b = 0;
while (x % 2 == 0) x /= 2, a++;
while (x % 3 == 0) x /= 3, b++;
c2[i] = a;
c3[i] = b;
}
memset(f, -0x3f, sizeof(f));
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
int a = c2[i], b = c3[i];
for (int j = k; j >= 1; --j) {
for (int c = C - 1; c >= a; --c) {
if (f[j - 1][c - a] >= 0)
f[j][c] = max(f[j][c], f[j - 1][c - a] + b);//关键一步!
}
}
}
int ans = 0;
for (int c = 0; c < C; ++c) {
if (f[k][c] >= 0)//注意必须加上此特判
ans = max(ans, min(c, f[k][c]));
}
cout << ans << endl;
return 0;
}
::::
给孩子关注、点赞吧。