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 <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int inv[17], niv[17];
struct seg {
int s, v;
seg *l, *r;
seg(int _s) {
s = _s, v = 0;
if (s) {
l = new seg(s - 1);
r = new seg(s - 1);
}
}
void update(int i) {
if (s) {
if (i < (1 << (s - 1))) l -> update(i), inv[s - 1] += r -> v;
else r -> update(i - (1 << (s - 1))), niv[s - 1] += l -> v;
v = l -> v + r -> v;
} else ++v;
}
} *root;
int main() {
int N, h;
scanf("%d", &N);
if (N == 0) {
printf("0\n11");
return 0;
}
int a[1 << N], ans[N];
vector<int> res[N];
fill_n(ans, N, 0);
for (int i = 0; i < (1 << N); ++i) scanf("%d", &h), a[h] = i;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < i; ++j) res[i].push_back(2);
root = new seg(N);
fill_n(inv, N, 0);
fill_n(niv, N, 0);
for (int j = 0; j < (1 << N); ++j) root -> update(a[j]);
for (int j = 0; j < N; ++j) {
res[i].push_back(2);
if (inv[j] > niv[j]) res[i].push_back(1), ans[i] += niv[j];
else ans[i] += inv[j];
}
for (int j = 0; j < (1 << N); ++j) a[j] = (a[j] >> 1) + ((1 & a[j]) << (N - 1));
}
printf("%d\n", *min_element(ans, ans + N));
for (int i: res[min_element(ans, ans + N) - ans]) printf("%d", i);
return 0;
}
Compilation message (stderr)
cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
cheerleaders.cpp:34:42: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | for (int i = 0; i < (1 << N); ++i) scanf("%d", &h), a[h] = i;
| ~~~~~^~~~~~~~~~
# | 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... |