Advertisement
TyFrendo

Untitled

Mar 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. int main()
  7. {
  8.     ios_base::sync_with_stdio(false);
  9.     cin.tie(NULL);
  10.  
  11. #ifndef ONLINE_JUDGE
  12.     freopen("split_in.txt", "r", stdin);
  13. #endif
  14.  
  15.     int n;
  16.     cin >> n;
  17.  
  18.     ll arr[n] = {};
  19.     cin >> arr[0];
  20.  
  21.     ll zeros = 0;
  22.     if (arr[0] == 0)
  23.         zeros = 1;
  24.  
  25.     for (int i = 1; i < n; ++i)
  26.     {
  27.         ll k;
  28.         cin >> k;
  29.  
  30.         arr[i] = arr[i - 1] + k;
  31.  
  32.         if (arr[i] == 0)
  33.             ++zeros;
  34.     }
  35.  
  36.     if (arr[n - 1] % 3 != 0 || n < 3)
  37.     {
  38.         cout << 0;
  39.         return 0;
  40.     }
  41.  
  42.     if (arr[n - 1] == 0)
  43.     {
  44.         zeros -= 2;
  45.         cout << zeros * (zeros + 1) / 2;
  46.         return 0;
  47.     }
  48.  
  49.     ll size = arr[n - 1] / 3;
  50.  
  51.     ll count1[n] = {}, temp[n + 1] = {};
  52.     int index1 = 0, index2 = 0;
  53.  
  54.     for (int i = 0; i < n; ++i)
  55.     {
  56.         if (arr[i] == size)
  57.         {
  58.             if (temp[index1] == 0)
  59.                 ++count1[index1];
  60.             else
  61.                 ++count1[++index1];
  62.         }
  63.         else if (arr[i] == 2 * size)
  64.         {
  65.             if (count1[index2 + 1] == 0)
  66.             {
  67.                 if (count1[index2] > 0)
  68.                     ++temp[index2];
  69.             }
  70.             else
  71.                 ++temp[++index2];
  72.         }
  73.     }
  74.  
  75.     int count2[index2 + 1] = {};
  76.  
  77.     for (int i = 0; i < index2 + 1; ++i)
  78.         count2[i] = temp[index2 - i] + temp[index2 - i + 1];
  79.  
  80.     ll sum = 0;
  81.  
  82.     for (int i = 0; i < index2 + 1; ++i)
  83.         sum += count1[i] * count2[index2 - i];
  84.  
  85.     cout << sum;
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement