题解:CF2187A Restricted Sorting
特判已经排序。先设
那么我们要求出连通块,考虑什么时候
所以连通块即所有
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int t, n, a[200005], b[200005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--) {
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i], b[i] = a[i];
sort(b + 1, b + n + 1);
int mn = b[1], mx = b[n], k = inf;
for(int i = 1; i <= n; i++)
if(a[i] ^ b[i])
k = min(k, max(mx - a[i], a[i] - mn));
cout << (k < inf ? k : -1) << '\n';
}
return 0;
}