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>
typedef long long ll;
using namespace std;
int n, h[1 << 17], at[1 << 17];
ll bit[(1 << 17) + 1];
void ins(int pos) {
for (; pos <= (1 << n); pos += pos & -pos) bit[pos]++;
}
ll query(int pos) {
ll ans = 0;
for (; pos; pos -= pos & -pos) ans += bit[pos];
return ans;
}
ll check(int mask, int shift) {
memset(bit, 0, sizeof bit);
ll ans = 0;
for (int i = (1 << n) - 1; ~i; i--) {
int masked = at[i] ^ mask;
masked = (masked >> shift) + ((masked & ((1 << shift) - 1)) << (n - shift));
ans += query(masked + 1);
ins(masked + 1);
}
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 0; i < (1 << n); i++) {
cin >> h[i];
at[h[i]] = i;
}
if (n <= 11) {
ll mn = LLONG_MAX;
string best_seq;
for (int mask = 0; mask < (1 << n); mask++) {
for (int shift : {0, n - 1}) {
int pot = check(mask, shift);
if (pot < mn) {
string seq;
for (int i = 0; i < n; i++) {
seq += "2";
if (mask & (1 << i)) seq += "1";
}
for (int i = 0; i < shift; i++) seq += "2";
mn = pot;
best_seq = seq;
}
}
}
cout << mn << '\n' << best_seq;
} else {
cout << 0 << '\n';
for (int i = 0; i < n; i++) {
cout << 2;
if (at[0] & (1 << i)) cout << 1;
}
for (int j = 0; j < __builtin_ctz(at[0] ^ at[1]); j++)
cout << 2;
}
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... |