Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define _test int _TEST; cin>>_TEST; while(_TEST--)
- #define pb push_back
- int main()
- {
- _test
- {
- int n;
- cin>>n;
- vector<int> a(n);
- for(auto &e: a)
- cin>>e;
- int ok = 1;
- for(int i=1; i<n; i++) ok &= (a[i] > a[i-1]);
- if(ok)
- {
- cout<<"0\n";
- continue;
- }
- vector<int> suff(n);
- vector<int> pref(n);
- for(int i=n-2; i>=0; i--) suff[i] = suff[i+1] + (a[i] >= a[i+1]);
- int x = 1;
- for(int i=1; i<n; i++)
- {
- if(a[i] <= a[i-1])
- pref[i] = x;
- else
- pref[i] = pref[i-1];
- x += (a[i] >= a[i-1]);
- }
- int ans = n;
- for(int i=0; i<n; i++)
- ans = min(ans, pref[i] + suff[i]);
- cout<<ans<<"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment