This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#define ll long long
#define maxn 150005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
//#include <bits/extc++.h>
//using namespace __gnu_pbds;
//template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//find by order, order of key
int a[maxn];
int bit[maxn], shift[17][maxn];
void modify(int ind, int val) {
for (;ind < maxn;ind += ind & (-ind)) bit[ind] += val;
}
int query(int ind) {
int ret = 0;
for (;ind > 0;ind -= ind & (-ind)) ret += bit[ind];
return ret;
}
ll solve(int pos, int type, int n) {
ll ret = 0;
type = n - 1 - type;
int dif = (pos - type + n) % n;
//cout << " " << dif << endl;
for (int i = 0;i < (1<<(n - 1 - type));i++) {
for (int j = 0;j < (1<<type);j++) {
int ind = shift[dif][(i<<(type+1)) + j];
//cout << ind << endl;
modify(a[ind], 1);
}
for (int j = 0;j < (1<<type);j++) {
int ind = shift[dif][(i<<(type+1)) + (1<<type) + j];
//cout << " " << ind << endl;
ret += query(a[ind]);
}
for (int j = 0;j < (1<<type);j++) {
int ind = shift[dif][(i<<(type+1)) + j];
modify(a[ind], -1);
}
}
return ret;
}
int main() {
io
int n;
cin >> n;
for (int i = 0;i < (1<<n);i++) {
cin >> a[i], a[i]++;
shift[0][i] = i;
int tmp = i;
//cout << tmp << " ";
for (int j = 1;j < n;j++) {
//cout << (tmp & (1<<(n - 1))) << endl;
int b = (tmp & (1<<(n - 1))) ? 1 : 0;
tmp -= (tmp & (1<<(n - 1)));
tmp = (tmp<<1) + b;
shift[j][i] = tmp;
//cout << shift[j][i] << " ";
}
//cout << endl;
}
/*
int op;
while (cin >> op) {
if (op == 1) {
for (int i = 0;i < (1<<(n - 1));i++) swap(a[i], a[i + (1<<(n - 1))]);
} else {
int b[1<<n];
for (int i = 0;i < (1<<n);i += 2) b[i / 2] = a[i];
for (int i = 1;i < (1<<n);i += 2) b[i / 2 + (1<<(n - 1))] = a[i];
for (int i = 0;i < (1<<n);i++) a[i] = b[i];
}
for (int i = 0;i < (1<<n);i++) cout << a[i] << " ";
cout << endl;
}
*/
ll best = 0;
int bind = 0, bp = 0;
for (int p = 0;p < n;p++) { //position of the highest bit is p
ll val = 0;
int num = 0;
for (int t = 0;t < n;t++) {
ll z = solve((p - t + n) % n, t, n);
ll o = (1LL<<t) * (1LL<<(n - 1 - t)) * (1LL<<(n - 1 - t)) - z;
//cout << t << " " << z << " " << o << endl;
val += max(z, o);
num<<=1;
if (o > z) {
num += 1;
}
}
//cout << p << " " << num << " " << val << endl;
if (val > best) {
best = val;
bind = num, bp = p;
}
}
//cout << best << endl;
int tot = 1<<n;
cout << (ll)tot * (tot - 1) / 2 - best << "\n";
if (n) {
bind = shift[(bp + 1) % n][bind];
cout << 2;
for (int i = 0;i < n;i++) {
if (bind & (1<<i)) cout << 1;
cout << 2;
}
for (int i = 0;i < bp;i++) cout << 2;
cout << endl;
}
}
/*
2
0 3 1 2
3
2 3 7 6 1 4 5 0
4
1 4 8 5 3 6 12 13 10 11 2 9 14 0 15 7
*/
# | 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... |