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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
//#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<iii, null_type, greater<iii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int N, ans = INT_MAX, ft[200005];
string out, curstr;
vector<int> curr;
set<vector<int> > seen;
vector<int> op1() {
vector<int> ret(N);
for (int i = 0; i < N; i++)
if (i < N / 2) ret[i + N / 2] = curr[i];
else ret[i - N / 2] = curr[i];
return ret;
}
vector<int> op2() {
vector<int> ret(N);
int oddcnt = 0, evencnt = 0;
for (int i = 0; i < N; i++)
if ((i + 1) & 1) ret[oddcnt++] = curr[i];
else ret[evencnt++ + N / 2] = curr[i];
return ret;
}
inline int ls(int x) { return x & -x; }
int qry(int p) {
int r = 0;
for (; p; p -= ls(p)) r += ft[p];
return r;
}
void upd(int p, int v) {
for (; p <= N; p += ls(p)) ft[p] += v;
}
bool setans() {
int tmp = 0;
for (int i = 1; i <= N; i++) ft[i] = 0;
for (int i = N - 1; i >= 0; i--) {
tmp += qry(curr[i]);
upd(curr[i], 1);
}
if (ans > tmp) {
ans = tmp;
return 1;
}
return 0;
}
void dfs() {
if (seen.count(curr)) return;
if (setans()) out = curstr;
seen.insert(curr);
vector<int> tmp1 = op1(), tmp2 = op2();
curr = tmp1;
curstr.pb('1');
dfs();
curstr.ppb();
curstr.pb('2');
curr = tmp2;
dfs();
curstr.ppb();
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
N = 1 << N;
for (int i = 1, x; i <= N; i++) {
cin >> x;
x++;
curr.pb(x);
}
dfs();
cout << ans << '\n';
if (out == "") cout << "11\n";
else cout << out << '\n';
}
Compilation message (stderr)
cheerleaders.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
91 | main() {
| ^~~~
# | 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... |