Advertisement
jbn6972

que4cpp

Oct 16th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define vi vector<int>
  6. #define vll vector<long long int>
  7.  
  8. ll solve(ll n, vll arr)
  9. {
  10.     ll res = 0;
  11.     ll sub_max;
  12.     for (ll i = 0; i < n - 2; i++)
  13.     {
  14.         sub_max = arr[i + 1];
  15.         if (arr[i] > arr[i + 1])
  16.         {
  17.             for (ll j = i + 2; j < n; j++)
  18.             {
  19.                 if (arr[i] > sub_max && arr[j] > sub_max)
  20.                 {
  21.                     res++;
  22.                 }
  23.                 sub_max = max(sub_max, arr[j]);
  24.             }
  25.         }
  26.     }
  27.     return res;
  28. }
  29.  
  30. int main()
  31. {
  32. #ifndef ONLINE_JUDGE
  33.     freopen("in.txt", "r", stdin);
  34.     freopen("out.txt", "w", stdout);
  35. #endif
  36.  
  37.     std::ios::sync_with_stdio(false);
  38.     ll n;
  39.     cin >> n;
  40.     vll arr;
  41.     ll num;
  42.     for (ll i = 0; i < n; i++)
  43.     {
  44.         cin >> num;
  45.         arr.push_back(num);
  46.     }
  47.     ll res = solve(n, arr);
  48.     cout << res + (n - 1);
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement