#include<bits/stdc++.h>
#define int long long
using namespace std ;
const int maxn = 22 ;
const int inf = 1e18 ;
int n, ans, v[maxn], go[maxn], test[maxn] ;
int32_t main(){
ios_base::sync_with_stdio(false) ; cin.tie(NULL) ;
cin >> n ;
for(int i = 1 ; i <= n ; i++) cin >> v[i] ;
for(int i = 1 ; i <= n ; i++) test[i] = i ;
reverse(test + 1, test + 1 + n) ;
ans = inf ;
do{
bool ok = 1 ;
for(int i = 1 ; i < n ; i++){
if(test[i] >= test[i+1] + 2) ok = 0 ;
}
if(!ok) continue ;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
if(test[i] == v[j]) go[i] = j ;
}
}
int ct = 0 ;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j < i ; j++){
if(go[i] < go[j]) ct++ ;
}
}
ans = min(ans, ct) ;
} while(next_permutation(test + 1, test + 1 + n)) ;
cout << ans << "\n" ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |