Ankit_132

D

Aug 31st, 2023
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8. #define pb     push_back
  9.  
  10. int main()
  11. {
  12.     _test
  13.     {
  14.         int n;
  15.         cin>>n;
  16.  
  17.         vector<int> a(n);
  18.         for(auto &e: a)
  19.             cin>>e;
  20.  
  21.         int ok = 1;
  22.         for(int i=1; i<n; i++)  ok &= (a[i] > a[i-1]);
  23.  
  24.         if(ok)
  25.         {
  26.             cout<<"0\n";
  27.             continue;
  28.         }
  29.  
  30.         vector<int> suff(n);
  31.         vector<int> pref(n);
  32.  
  33.         for(int i=n-2; i>=0; i--)   suff[i] = suff[i+1] + (a[i] >= a[i+1]);
  34.         int x = 1;
  35.  
  36.         for(int i=1; i<n; i++)
  37.         {
  38.             if(a[i] <= a[i-1])
  39.                 pref[i] = x;
  40.             else
  41.                 pref[i] = pref[i-1];
  42.  
  43.             x += (a[i] >= a[i-1]);
  44.         }
  45.  
  46.         int ans = n;
  47.  
  48.         for(int i=0; i<n; i++)
  49.             ans = min(ans, pref[i] + suff[i]);
  50.  
  51.         cout<<ans<<"\n";
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment