Untitled
unknown
c_cpp
a year ago
1.0 kB
13
Indexable
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool can_reduce_to_zero(const vector<int>& arr, int x, int y, int k) {
int required_ops = 0;
for (int element : arr) {
if (element <= k * y) {
continue;
}
int extra_ops = (element - k * y + x - y - 1) / (x - y);
required_ops += extra_ops;
if (required_ops > k) {
return false;
}
}
return true;
}
int min_operations_to_zero(const vector<int>& arr, int x, int y) {
int left = 0, right = *max_element(arr.begin(), arr.end()) / min(x, y) + 1;
while (left < right) {
int mid = (left + right) / 2;
if (can_reduce_to_zero(arr, x, y, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
int main() {
vector<int> arr = {10, 20, 30};
int x = 3, y = 1;
cout << min_operations_to_zero(arr, x, y) << endl;
return 0;
}
Editor is loading...
Leave a Comment