题解
2025-06-17 15:31:09
发布于:浙江
4阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 计算使用第二种魔法后能减少的阻碍值
int calculateReduction(int num) {
int mod3 = num % 3;
if (mod3 == 0) {
// 对3取模后为0,使用第二种魔法不会减少阻碍值
return 0;
} else if (mod3 == 1) {
// 对3取模后为1,使用第二种魔法可以减少1
return 1;
} else {
// 对3取模后为2,使用第二种魔法可以减少2
return 2;
}
}
int main() {
int n, k;
cin >> n >> k;
vector<int> enemies(n);
int total = 0;
// 读取每个敌人的战斗力并计算初始总阻碍值(已使用第一种魔法)
for (int i = 0; i < n; i++) {
cin >> enemies[i];
enemies[i] %= 3;
total += enemies[i];
}
// 计算每个元素使用第二种魔法能减少的阻碍值
vector<int> reductions;
for (int val : enemies) {
reductions.push_back(calculateReduction(val));
}
// 按减少量从大到小排序
sort(reductions.rbegin(), reductions.rend());
// 选择前k个最大的减少量
int useK = min(k, (int)reductions.size());
for (int i = 0; i < useK; i++) {
total -= reductions[i];
}
// 输出最小总阻碍值
cout << total << endl;
return 0;
}
这里空空如也
有帮助,赞一个