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>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 17;
int a[1 << N];
vi who[1 << N][N];
signed main()
{
IO_OP;
int n;
cin >> n;
if(n == 0){
cout << 0 << endl;
cout << endl;
return 0;
}
for(int i = 0; i < (1 << n); i++) {
int x;
cin >> x;
a[x] = i;
}
auto shift_right = [&]() {
cout << 2;
};
auto flip_bit = [&](int bit) {
for(int i = 0; i < bit + 1; i++) shift_right();
cout << 1;
for(int i = 0; i < n - bit - 1; i++) shift_right();
};
auto xor_val = [&](int val) {
for(int i = 0; i < n; i++) if(val >> i & 1)
flip_bit(i);
};
ll best_inv = 1e18, best_shift = 0, best_xor = 0;
for(int shift = 0; shift < n; shift++) {
for(int i = 0; i < n; i++)
for(int j = 0; j < (1 << (n - i)); j++)
who[j][i].clear();
for(int j = 0; j < (1 << n); j++)
for(int i = 0; i < n; i++)
who[a[j] >> i][i].PB(j);
auto count_inv = [&](vi& a, vi& b) {
ll ans = 0;
for(int i = 0, j = 0; i < a.size(); i++) {
while(j < b.size() && b[j] < a[i]) j++;
ans += j;
}
return ans;
};
ll val = 0, inv = 0;
for(int i = n - 1; i >= 0; i--) {
ll one = 0, zero = 0;
for(int prefix = 0; prefix < (1 << (n - i)); prefix += 2) {
int mask1 = prefix, mask2 = prefix + 1;
ll cur_zero = count_inv(who[mask1][i], who[mask2][i]);
// ll cur_one = count_inv(who[mask2][i], who[mask1][i]);
ll cur_one = (ll) who[mask1][i].size() * who[mask2][i].size() - cur_zero; // const opt
one += cur_one, zero += cur_zero;
}
if(one < zero) val |= 1 << i;
inv += min(one, zero);
}
if(inv < best_inv) {
best_inv = inv;
best_shift = shift;
best_xor = val;
}
for(int i = 0; i < (1 << n); i++)
a[i] = (a[i] >> 1) | ((a[i] & 1) << (n - 1)); // shift right
}
cout << best_inv << endl;
for(int i = 0; i < best_shift; i++) shift_right();
xor_val(best_xor);
cout << endl;
}
Compilation message (stderr)
cheerleaders.cpp: In lambda function:
cheerleaders.cpp:61:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = 0, j = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
cheerleaders.cpp:62:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | while(j < b.size() && b[j] < a[i]) j++;
| ~~^~~~~~~~~~
# | 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... |