#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n;
vector<int> idx;
int cost(vector<int> &p) {
// check if the arrangement is valid
for (int i = 0; i < n-1; i++) {
if (p[i] >= p[i+1]+2) return INF;
}
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if (idx[p[j]] < idx[p[i]]) res++;
}
}
return res;
}
int main() {
// n <= 20
cin >> n;
idx.resize(n+1);
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
idx[h[i]] = i;
}
int ans = INT_MAX;
for (int mask = 0; mask < (1<<n); mask++) {
vector<int> flip;
for (int i = 0; i < n; i++) {
if (mask & (1<<i)) flip.push_back(i);
}
if (flip.size() == 0 || (flip.size() > 1 && flip[0] == 0)) continue;
vector<int> p(n);
iota(p.begin(), p.end(), 1);
int prev = 0;
for (int i = 0; i < flip.size(); i++) {
reverse(p.begin()+prev, p.begin()+flip[i]);
prev = flip[i];
}
reverse(p.begin()+prev, p.end());
ans = min(ans, cost(p));
prev = 0;
for (int i = 0; i < flip.size(); i++) {
reverse(p.begin()+prev, p.begin()+flip[i]);
prev = flip[i];
}
reverse(p.begin()+prev, p.end());
}
cout << ans << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:42:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < flip.size(); i++) {
| ~~^~~~~~~~~~~~~
Main.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < flip.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
106 ms |
504 KB |
Output is correct |
12 |
Correct |
183 ms |
336 KB |
Output is correct |
13 |
Correct |
197 ms |
336 KB |
Output is correct |
14 |
Correct |
384 ms |
336 KB |
Output is correct |
15 |
Correct |
399 ms |
420 KB |
Output is correct |
16 |
Correct |
386 ms |
336 KB |
Output is correct |
17 |
Correct |
364 ms |
336 KB |
Output is correct |
18 |
Correct |
405 ms |
428 KB |
Output is correct |
19 |
Correct |
384 ms |
340 KB |
Output is correct |
20 |
Correct |
405 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
106 ms |
504 KB |
Output is correct |
12 |
Correct |
183 ms |
336 KB |
Output is correct |
13 |
Correct |
197 ms |
336 KB |
Output is correct |
14 |
Correct |
384 ms |
336 KB |
Output is correct |
15 |
Correct |
399 ms |
420 KB |
Output is correct |
16 |
Correct |
386 ms |
336 KB |
Output is correct |
17 |
Correct |
364 ms |
336 KB |
Output is correct |
18 |
Correct |
405 ms |
428 KB |
Output is correct |
19 |
Correct |
384 ms |
340 KB |
Output is correct |
20 |
Correct |
405 ms |
340 KB |
Output is correct |
21 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
106 ms |
504 KB |
Output is correct |
12 |
Correct |
183 ms |
336 KB |
Output is correct |
13 |
Correct |
197 ms |
336 KB |
Output is correct |
14 |
Correct |
384 ms |
336 KB |
Output is correct |
15 |
Correct |
399 ms |
420 KB |
Output is correct |
16 |
Correct |
386 ms |
336 KB |
Output is correct |
17 |
Correct |
364 ms |
336 KB |
Output is correct |
18 |
Correct |
405 ms |
428 KB |
Output is correct |
19 |
Correct |
384 ms |
340 KB |
Output is correct |
20 |
Correct |
405 ms |
340 KB |
Output is correct |
21 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
106 ms |
504 KB |
Output is correct |
12 |
Correct |
183 ms |
336 KB |
Output is correct |
13 |
Correct |
197 ms |
336 KB |
Output is correct |
14 |
Correct |
384 ms |
336 KB |
Output is correct |
15 |
Correct |
399 ms |
420 KB |
Output is correct |
16 |
Correct |
386 ms |
336 KB |
Output is correct |
17 |
Correct |
364 ms |
336 KB |
Output is correct |
18 |
Correct |
405 ms |
428 KB |
Output is correct |
19 |
Correct |
384 ms |
340 KB |
Output is correct |
20 |
Correct |
405 ms |
340 KB |
Output is correct |
21 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |