답안 #493928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
493928 2021-12-13T11:48:57 Z balbit Cheerleaders (info1cup20_cheerleaders) C++14
100 / 100
544 ms 6420 KB
#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;

}
# 결과 실행 시간 메모리 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 0 ms 204 KB Correct!
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Correct!
2 Correct 1 ms 204 KB Correct!
3 Correct 1 ms 204 KB Correct!
4 Correct 0 ms 204 KB Correct!
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 332 KB Correct!
2 Correct 5 ms 332 KB Correct!
3 Correct 5 ms 332 KB Correct!
4 Correct 6 ms 332 KB Correct!
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 1612 KB Correct!
2 Correct 79 ms 1612 KB Correct!
3 Correct 172 ms 2972 KB Correct!
# 결과 실행 시간 메모리 Grader output
1 Correct 375 ms 5648 KB Correct!
2 Correct 405 ms 6276 KB Correct!
3 Correct 544 ms 6280 KB Correct!
4 Correct 510 ms 6420 KB Correct!
5 Correct 513 ms 6360 KB Correct!