-
Notifications
You must be signed in to change notification settings - Fork 3
/
2333.cpp
49 lines (49 loc) · 1.56 KB
/
2333.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution {
public:
bool criteria(vector<long long>& diff, long long tobe, long long maxCount) {
long long count = 0;
for (auto& num : diff) {
if (num > tobe) count += (num - tobe);
}
return count <= maxCount;
}
long long remainConut(vector<long long>& diff, long long tobe, long long maxCount) {
long long count = 0;
for (auto& num : diff) {
if (num > tobe) count += (num - tobe);
}
return maxCount - count;
}
long long minSumSquareDiff(vector<int>& nums1, vector<int>& nums2, int k1, int k2) {
int n = nums1.size();
vector<long long> diff(n, 0);
for (int i = 0; i < n; ++i) {
diff[i] = abs(nums1[i] - nums2[i]);
}
sort(diff.begin(), diff.end());
long long left = 0;
long long right = diff[diff.size() - 1];
long long K = k1 + k2;
while (left < right) {
long long mid = left + (right - left) / 2;
if (criteria(diff, mid, K)) right = mid;
else left = mid + 1;
}
long long remainK = remainConut(diff, left, K);
if (left == 0) return 0;
long long res = 0;
for (auto& num : diff) {
if (num >= left) {
if (remainK) {
res += (left - 1) * (left - 1);
remainK--;
}
else {
res += left * left;
}
}
else res += num * num;
}
return res;
}
};