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 main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
--h[i];
}
vector<int> pos(n);
for (int i = 0; i < n; i++) {
pos[h[i]] = i;
}
vector<int> p(n);
iota(p.begin(), p.end(), 0);
int ans = 1e9;
do {
bool valid = true;
for (int i = 0; i + 1 < n; i++) {
valid = (valid & (p[i] < p[i + 1] || p[i] == p[i + 1] + 1));
}
if (!valid) {
continue;
}
int nans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (pos[p[i]] > pos[p[j]]) {
nans += 1;
}
}
}
ans = min(ans, nans);
} while (next_permutation(p.begin(), p.end()));
cout << ans << '\n';
return 0;
}
# | 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... |