#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 |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
7 ms |
324 KB |
Output is correct |
8 |
Correct |
7 ms |
212 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
7 ms |
324 KB |
Output is correct |
8 |
Correct |
7 ms |
212 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
320 KB |
Output is correct |
11 |
Execution timed out |
5038 ms |
212 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
7 ms |
324 KB |
Output is correct |
8 |
Correct |
7 ms |
212 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
320 KB |
Output is correct |
11 |
Execution timed out |
5038 ms |
212 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
7 ms |
324 KB |
Output is correct |
8 |
Correct |
7 ms |
212 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
320 KB |
Output is correct |
11 |
Execution timed out |
5038 ms |
212 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
7 ms |
324 KB |
Output is correct |
8 |
Correct |
7 ms |
212 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
320 KB |
Output is correct |
11 |
Execution timed out |
5038 ms |
212 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |