减成负数之前的最大操作次数
概述
给你 5 个正数,每次选择其中 4 个全部 -1,不能减到 -1,也就是最小值为 0,求最大操作次数。
贪心法
我不理解为啥一个用例也没过。。。
#include <bits/stdc++.h>
using namespace std;
int main() {
int T; cin >> T;
for (int t = 0; t < T; t ++) {
vector<long> nums(5);
for (int i = 0; i < 5; i ++) cin >> nums[i];
sort(nums.begin(), nums.end());
long a = nums[0], b = nums[1];
int count = 0;
count = b - a;
for (int i = 1; i < 5; i ++) {
nums[i] -= count;
}
long c = nums[2];
// now we have: a a c * *
if (c >= 2 * a) {
count += 2 * a;
// now we have 0 0 0 * *
} else {
count += c;
}
cout << count << endl;
}
}
Links: 减成负数之前的最大操作次数