Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int NumTriplesWithSumX(
- vector<int>& a,
- vector<int>& b,
- vector<int>& c,
- int x
- )
- {
- /*
- int l1 = v1.size();
- int l2 = v2.size();
- int l3 = v3.size();
- vector<int> *p_short, *p_long1, *p_long2;
- if (l1 <= l2 && l1 <= l3) {
- p_short = &v1;
- p_long1 = &v2;
- p_long2 = &v3;
- } else if (l2 <= l1 && l2 <= l3) {
- p_short = &v2;
- p_long1 = &v1;
- p_long2 = &v3;
- } else { // v3 is the shortest
- p_short = &v3;
- p_long1 = &v1;
- p_long2 = &v2;
- }
- vector<int>& a = *p_long1;
- vector<int>& b = *p_long2;
- vector<int>& c = *p_short;
- */
- sort(a.begin(), a.end());
- sort(b.begin(), b.end());
- reverse(b.begin(), b.end());
- int ans = 0;
- const int n = a.size();
- const int m = b.size();
- for (auto num : c) {
- int required_value = x - num;
- int i = 0;
- int j = 0;
- while (i < n && j < m) {
- if (a[i] + b[j] < required_value){
- ++i;
- } else if (a[i] + b[j] > required_value) {
- ++j;
- } else {
- int flag_a = i++;
- while (i < n && a[i] == a[i - 1]) {
- i++;
- }
- int flag_b = j++;
- while (j < m && b[j] == b[j - 1]) {
- j++;
- }
- ans += (i - flag_a) * (j - flag_b);
- }
- }
- }
- return ans;
- }
- int main() {
- {
- vector<int> a = {1, 1, 1, 2, 2, 3, 4, 4};
- vector<int> b = {8,7,5,4,4,4,3,3,1,1,1,0};
- vector<int> c = {0};
- cout << NumTriplesWithSumX(a, b, c, 5) << endl;
- }
- {
- vector<int> b = {1, 1, 1, 2, 2, 3, 4, 4};
- vector<int> c = {8,7,5,4,4,4,3,3,1,1,1,0};
- vector<int> a = {0};
- cout << NumTriplesWithSumX(a, b, c, 5) << endl;
- }
- {
- vector<int> a = {1, 1, 1, 1, 1, 1};
- vector<int> b = {2, 2, 2, 2};
- vector<int> c = {3, 3};
- cout << NumTriplesWithSumX(a, b, c, 6) << endl;
- }
- {
- vector<int> a = {};
- vector<int> b = {1, 1, 1, 1};
- vector<int> c = {0};
- cout << NumTriplesWithSumX(a, b, c, 1) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement