This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n, a[5001], b[5001], l, mp[5001], ta[5001], t, ans=2e9;
int main() {
cin >> n;
for (int i=1; i<=n; i++) cin >> a[i];
for (int mask=0; mask<(1<<n); mask++){
if (__builtin_popcount(mask)%2) continue;
for (int i=1; i<=n; i++) b[i]=i;
l=-1;
for (int i=0; i<n; i++){
if (mask&(1<<i)){
if (l==-1) l=i;
else{
reverse(b+l+1, b+i+2);
l=-1;
}
}
}
for (int i=1; i<=n; i++) mp[b[i]]=i;
for (int i=1; i<=n; i++) ta[i]=mp[a[i]];
t=0;
for (int i=1; i<=n; i++){
for (int j=1; j<i; j++){
if (ta[j]>ta[i]) t++;
}
}
ans=min(ans, t);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |