Submission #493928

#TimeUsernameProblemLanguageResultExecution timeMemory
493928balbitCheerleaders (info1cup20_cheerleaders)C++14
100 / 100
544 ms6420 KiB
#include <bits/stdc++.h> using namespace std; #define int ll #define ll long long #define f first #define s second #define pii pair<ll, ll> #define REP(i,n) for (int i = 0; i<n; ++i) #define REP1(i,n) for (int i = 1; i<=n; ++i) #define MX(a,b) a = max(a,b) #define MN(a,b) a = min(a,b) #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int)((x).size()) #define pb push_back #ifdef BALBIT #define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__) template<typename T> void _do( T && x) {cerr<<x<<endl;} template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);} #else #define bug(...) #define endl '\n' #endif // BALBIT int n; inline int cyc(int x) { return (x>>1) | ((x&1)<<(n-1)); } ll noflip[20], flip[20]; void work(vector<int> p, int k) { // looking at the (n-1-k)th bit if (k==n) return; int n0=0,n1=0; vector<int> g0, g1; g0.reserve(SZ(p)/2); g1.reserve(SZ(p)/2); for (int x : p) { if (x & (1<<(n-1-k))) { flip[k] += n0; ++n1; g0.pb(x); }else{ noflip[k] += n1; ++n0; g1.pb(x); } } work(g0, k+1); work(g1, k+1); } const ll inf = 1ll<<60; signed main(){ ios::sync_with_stdio(0), cin.tie(0); bug(1,2); int k; cin>>n; k = n; if (n == 0) { cout<<"0\n\n"; return 0; } // bug(cyc(10), cyc(cyc(10))); vector<int> a(1<<n); REP(i,(1<<n)) { int b; cin>>b; a[b]=i; } int bst = -1; ll bval = inf; for (int ns = 0; ns < k; ++ns) { memset(noflip, 0, sizeof noflip); memset(flip, 0, sizeof flip); work(a, 0); ll mval = 0; // for (int t : a) bug(t); // cout<<endl; REP(i, k) { mval += min(flip[i], noflip[i]); bug(i, flip[i], noflip[i]); } bug(ns, mval); if (mval < bval) { bval = mval; bst = ns; } for (int &t : a) { t = cyc(t); } } bug(bst, bval); string re; for (int &t : a) { REP(b, bst) t = cyc(t); } REP(b, bst) re += "2"; memset(noflip, 0, sizeof noflip); memset(flip, 0, sizeof flip); work(a, 0); REP(i, k) { re += "2"; if (flip[k-i-1] < noflip[k-i-1]) { re += "1"; } // mval += min(flip[i], noflip[i]); } cout<<bval<<endl<<re<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...