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>
#pragma gcc optimize("Ofast")
#define ll long long
#define pb emplace_back
#define All(x) x.begin(), x.end()
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }
const int MAXN = 17;
int N, M;
ll ans;
pii op;
string ops;
vector <int> a, c;
vector <ll> val;
inline int lb(int a) {
return a &- a;
}
void init() {
a.resize(M);
c.resize(M);
val.resize(N);
}
class Bit {
private:
int arr[1<<MAXN+1];
public:
inline void mdy(int a, int k) {
for (int i = a; i <= M; i+=lb(i))
arr[i] += k;
}
inline int qry(int a) {
int s = 0;
for (int i = a; i; i-=lb(i))
s += arr[i];
return s;
}
} bit;
ll cnt_inv(vector <int> c) {
ll s = 0;
vector <int> del;
for (int i = 0; i < M; i++) {
s += i - bit.qry(c[i]);
bit.mdy(c[i]+1, 1);
}
for (int i = 0; i < M; i++)
bit.mdy(i+1, -1);
return s;
}
void build() {
for (int i = 0; i < N; i++) {
vector <vector<int>> arr(1<<(N-i));
for (vector<int> &v : arr) v.reserve(1<<i);
val[i] = 0;
for (int j = 0; j < M; j++)
arr[c[j]/(1<<i)].pb(j);
for (int j = 0; j < (1<<(N-i)); j+=2) {
for (int k : arr[j+1]) {
val[i] += arr[j].end() - lower_bound(All(arr[j]), k);
}
}
}
}
string solve(pii op) {
string str;
for (int i = 0; i < op.ff; i++)
str.push_back('2');
for (int i = 0; i < N; i++) {
str.push_back('2');
if ((1<<i) & op.ss) {
str.push_back('1');
}
}
return str;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N;
M = (1<<N);
init();
for (int i = 0; i < M; i++)
cin >> a[i], c[a[i]] = i;
ans = cnt_inv(c);
for (int j = 0; j < N; j++) {
build();
ll sum = cnt_inv(c);
int x = 0;
for (int i = 1; i < M; i++) {
int p = lb(i);
x ^= p;
ll O = (ll) M * p / 2;
sum += O - 2 * val[__lg(p)];
val[__lg(p)] = O - val[__lg(p)];
if (sum < ans) {
ans = sum, op = pii(j, x);
}
}
for (int i = 0; i < M; i++) {
c[i] = M / 2 * (c[i] & 1) + c[i] / 2;
}
}
cout << ans << '\n';
cout << solve(op) << '\n';
return 0;
}
Compilation message (stderr)
cheerleaders.cpp:2: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
2 | #pragma gcc optimize("Ofast")
|
cheerleaders.cpp:35:18: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
35 | int arr[1<<MAXN+1];
| ~~~~^~
# | 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... |