Advertisement
goshansmails

Untitled

Mar 16th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int NumTriplesWithSumX(
  8.         vector<int>& a,
  9.         vector<int>& b,
  10.         vector<int>& c,
  11.         int x
  12. )
  13. {
  14.     /*
  15.     int l1 = v1.size();
  16.     int l2 = v2.size();
  17.     int l3 = v3.size();
  18.  
  19.     vector<int> *p_short, *p_long1, *p_long2;
  20.  
  21.     if (l1 <= l2 && l1 <= l3) {
  22.         p_short = &v1;
  23.         p_long1 = &v2;
  24.         p_long2 = &v3;
  25.     } else if (l2 <= l1 && l2 <= l3) {
  26.         p_short = &v2;
  27.         p_long1 = &v1;
  28.         p_long2 = &v3;
  29.     } else { // v3 is the shortest
  30.         p_short = &v3;
  31.         p_long1 = &v1;
  32.         p_long2 = &v2;
  33.     }
  34.  
  35.     vector<int>& a = *p_long1;
  36.     vector<int>& b = *p_long2;
  37.     vector<int>& c = *p_short;
  38. */
  39.     sort(a.begin(), a.end());
  40.     sort(b.begin(), b.end());
  41.     reverse(b.begin(), b.end());
  42.  
  43.     int ans = 0;
  44.     const int n = a.size();
  45.     const int m = b.size();
  46.  
  47.     for (auto num : c) {
  48.         int required_value = x - num;
  49.         int i = 0;
  50.         int j = 0;
  51.         while (i < n && j < m) {
  52.             if (a[i] + b[j] < required_value){
  53.                 ++i;
  54.             } else if (a[i] + b[j] > required_value) {
  55.                 ++j;
  56.             } else {
  57.                 int flag_a = i++;
  58.                 while (i < n && a[i] == a[i - 1]) {
  59.                     i++;
  60.                 }
  61.                 int flag_b = j++;
  62.                 while (j < m && b[j] == b[j - 1]) {
  63.                     j++;
  64.                 }
  65.                 ans += (i - flag_a) * (j - flag_b);
  66.             }
  67.         }
  68.     }
  69.  
  70.     return ans;
  71. }
  72. int main() {
  73.     {
  74.         vector<int> a = {1, 1, 1, 2, 2, 3, 4, 4};
  75.         vector<int> b = {8,7,5,4,4,4,3,3,1,1,1,0};
  76.         vector<int> c = {0};
  77.         cout << NumTriplesWithSumX(a, b, c, 5) << endl;
  78.     }
  79.     {
  80.         vector<int> b = {1, 1, 1, 2, 2, 3, 4, 4};
  81.         vector<int> c = {8,7,5,4,4,4,3,3,1,1,1,0};
  82.         vector<int> a = {0};
  83.         cout << NumTriplesWithSumX(a, b, c, 5) << endl;
  84.     }
  85.     {
  86.         vector<int> a = {1, 1, 1, 1, 1, 1};
  87.         vector<int> b = {2, 2, 2, 2};
  88.         vector<int> c = {3, 3};
  89.         cout << NumTriplesWithSumX(a, b, c, 6) << endl;
  90.     }
  91.     {
  92.         vector<int> a = {};
  93.         vector<int> b = {1, 1, 1, 1};
  94.         vector<int> c = {0};
  95.         cout << NumTriplesWithSumX(a, b, c, 1) << endl;
  96.     }
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement