-
Notifications
You must be signed in to change notification settings - Fork 20
/
1538A.cpp
39 lines (33 loc) · 932 Bytes
/
1538A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t, n;
cin>>t;
while(t--){
cin>>n;
int arr[n];
for(int i = 0; i < n; i++){
cin>>arr[i];
}
int mx = distance(arr, max_element(arr, arr + n)) + 1;
int mn = distance(arr, min_element(arr, arr + n)) + 1;
int mx_b = abs(distance(arr + n, max_element(arr, arr + n)));
int mn_b = abs(distance(arr + n, min_element(arr, arr + n)));
int mrr[4] = {mx, mn, mx_b, mn_b};
int steps = 0;
steps += *min_element(mrr, mrr + 4);
if(*min_element(mrr, mrr+4) == mx || *min_element(mrr, mrr+4) == mx_b){
steps += min(min(abs(mn - mx), mn_b), min(mn, mn_b));
}
else{
steps += min(min(abs(mx - mn), mx_b), min(mx, mx_b));
}
cout<<steps<<endl;
}
}