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;
//#include "debug.h"
void solve() {
int k;
cin >> k;
int n = 1 << k;
vector<int> arr(n), f(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (n == 0) {
cout << 0 << '\n' << '\n';
return;
}
long long ans = (long long) 1e18;
for (int iter = 0; iter < k; iter++) {
vector<int> a(n);
for (int i = 0; i < n; i++) {
a[i / 2 + (i % 2 ? (1 << (k - 1)) : 0)] = arr[i];
}
arr = a;
long long res = 0;
for (int len = 1; len <= n / 2; len *= 2) {
long long L = 0, R = 0;
for (int i = 0; i < n; i += len * 2) {
long long val = 0;
for (int j = i + len; j < i + len * 2; j++) {
for (int x = a[j]; x < n; x |= (x + 1)) {
f[x]++;
}
}
for (int j = i; j < i + len; j++) {
val += len;
for (int x = a[j]; x >= 0; x = (x & (x + 1)) - 1) {
val -= f[x];
}
}
for (int j = i + len; j < i + len * 2; j++) {
for (int x = a[j]; x < n; x |= (x + 1)) {
f[x]--;
}
}
L += val;
R += len * 1ll * len - val;
}
res += min(L, R);
}
ans = min(ans, res);
}
cout << ans << '\n';
// set<vector<int>> usd;
// auto dfs = [&] (auto&& self, vector<int> ord) {
// if (usd.find(ord) != usd.end()) {
// return;
// }
// usd.insert(ord);
//// if (ord[0] == 0 || ord[0] == n - 1) debug(ord);
// vector<int> a(n);
// for (int i = 0; i < n; i++) {
// if (i < n / 2) a[i] = ord[i + n / 2];
// else a[i] = ord[i - n / 2];
// }
// self(self, a);
// for (int i = 0; i < n; i++) {
// if (i < n / 2) a[i] = ord[i * 2];
// else a[i] = ord[1 + (i - n / 2) * 2];
// }
// self(self, a);
// };
// vector<int> ord(n);
// std::iota(ord.begin(), ord.end(), 0);
// dfs(dfs, ord);
// cout << usd.size();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# | 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... |