题目
思路:
赛时代码(先求右起最长有序区间长度,再求左边最小值是否小于等于右边有序区间左端点的数)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6 + 5;
int a[maxn];
int n;
map<int, int> pos;
void solve(){
int i, j;
cin >> n;
bool mul = 0;
for(i = 1; i <= n; i++){
cin >> a[i];
}
int cnt = 0;
for(i = n - 1; i >= 1; i--){
if(a[i] <= a[i + 1]) cnt++;
else break;
}
//cout << cnt << '\n';
if(cnt == n - 1){
cout << 0 << '\n';
return;
}
int r = a[n - cnt];
int l = 1e18;
for(i = 1; i <= n - cnt - 1; i++){
l = min(l, a[i]);
}
if(l <= r){
cout << -1 << '\n';
return;
}
cout << n - cnt - 1 << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}