#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define ceil(a, b) ((a + b - 1) / (b))
#define all(x) x.begin(), x.end()
#define lld long double
#define int long long int
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
using namespace std;
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
#ifdef wiwihorz
#define print(a...) cerr<<"Line "<<__LINE__<<":",kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L)==R], ++L;}
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define x first
#define y second
namespace solver {
int n, tot, mask;
vector<int> a, b, cnt;
void init_(int _n) {
n = _n;
tot = 1 << n;
mask = tot - 1;
a.assign(tot, 0);
b.assign(tot, 0);
cnt.assign(tot, 0);
}
string get_ans(pii opt) {
string s = "2";
rep(i, 1, opt.x) s += "2";
rep(i, 0, n - 1) {
if((opt.y >> i) & 1) s += "1";
if(i != n - 1) s += "2";
}
return s;
}
void solve() {
pii opt = {0, 0};
int ans = INF;
vector<int> tp(tot, 0);
rep(i, 0, tot - 1) tp[a[i]] = i;
a = tp;
rep(i, 0, n - 1) {
pii p = {i, 0};
int best = 0;
rep(j, 0, tot - 1) b[j] = mask
& ((a[j] >> i) | (a[j] << (n - i)));
rep(j, 1, n) {
int L = 0, R = 0, bot = 1 << (n - j);
int val = ((1 << j) - 1) << (n - j);
cnt.assign(tot, 0);
rep(k, 0, tot - 1) {
int cur = b[k] & val;
int to = cur ^ bot;
if(cur > to) R += cnt[to];
else L += cnt[to];
cnt[cur] ++;
}
if(L > R) p.y |= bot;
best += min(L, R);
}
if(best < ans) opt = p, ans = best;
}
cout << ans << "\n";
cout << get_ans(opt) << "\n";
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n; cin >> n;
init_(n);
rep(i, 0, tot - 1) cin >> a[i];
if(n == 0) cout << "0\n";
else solve();
return 0;
}
Compilation message
cheerleaders.cpp:5: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
5 | #pragma loop-opt(on)
|
cheerleaders.cpp:21:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
21 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L)==R], ++L;}
| ^~~~
cheerleaders.cpp:21:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
21 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L)==R], ++L;}
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Correct! |
2 |
Correct |
1 ms |
204 KB |
Correct! |
3 |
Correct |
1 ms |
204 KB |
Correct! |
4 |
Correct |
1 ms |
204 KB |
Correct! |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Correct! |
2 |
Correct |
0 ms |
204 KB |
Correct! |
3 |
Correct |
0 ms |
204 KB |
Correct! |
4 |
Correct |
1 ms |
204 KB |
Correct! |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Correct! |
2 |
Correct |
2 ms |
332 KB |
Correct! |
3 |
Correct |
2 ms |
332 KB |
Correct! |
4 |
Correct |
2 ms |
332 KB |
Correct! |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
1364 KB |
Correct! |
2 |
Correct |
40 ms |
1356 KB |
Correct! |
3 |
Correct |
97 ms |
2388 KB |
Correct! |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
4428 KB |
Correct! |
2 |
Correct |
245 ms |
5168 KB |
Correct! |
3 |
Correct |
432 ms |
5216 KB |
Correct! |
4 |
Correct |
401 ms |
5212 KB |
Correct! |
5 |
Correct |
410 ms |
5216 KB |
Correct! |