답안 #807684

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
807684 2023-08-04T21:27:29 Z SorahISA Cheerleaders (info1cup20_cheerleaders) C++17
54 / 100
1891 ms 6256 KB
#ifndef SorahISA
#define SorahISA
#include SorahISA __FILE__ SorahISA

void solve() {
    int N; cin >> N;
    int64_t NN = (1 << N);
    
    vector<int> A(NN), inv(NN);
    for (int i = 0; i < NN; ++i) cin >> A[i], inv[A[i]] = i;
    
    auto ch1 = [&](vector<int> &Z) {
        for (int i = 0; i < NN/2; ++i) swap(Z[i], Z[i+NN/2]);
        // cerr << "\u001b[34m";
        // for (int i = 0; i < NN; ++i) cerr << Z[i] << " \n"[i == NN-1];
        // cerr << "\u001b[0m";
    };
    
    auto ch2 = [&](vector<int> &Z) {
        static vector<int> B(NN);
        for (int i = 0; i < NN; i += 2) B[i/2] = Z[i];
        for (int i = 1; i < NN; i += 2) B[i/2+NN/2] = Z[i];
        Z = B;
        // cerr << "\u001b[35m";
        // for (int i = 0; i < NN; ++i) cerr << Z[i] << " \n"[i == NN-1];
        // cerr << "\u001b[0m";
    };
    
    auto calc_inv = [&](vector<int> &id) -> int64_t {
        static vector<int> BIT(NN+1);
        fill(ALL(BIT), 0);
        int64_t cnt = 0;
        for (int i = NN-1; i >= 0; --i) {
            for (int j = A[id[i]]+1; j >   0; j -= j&-j) cnt += BIT[j];
            for (int j = A[id[i]]+1; j <= NN; j += j&-j) ++BIT[j];
        }
        return cnt;
    };
    
    auto calc_cost = [&](vector<int> &id, int blk) -> bool {
        static vector<int> BIT(NN+1);
        fill(ALL(BIT), 0);
        int64_t cnt0 = 0, cnt1 = 0;
        for (int i = 0; i < NN; i += 2*blk) {
            int64_t tmp = 0;
            for (int j = 0; j < blk; ++j) {
                for (int k = A[id[i+j]]+1; k <= NN; k += k&-k) ++BIT[k];
            }
            for (int j = blk; j < 2*blk; ++j) {
                for (int k = A[id[i+j]]+1; k >   0; k -= k&-k) tmp += BIT[k];
            }
            for (int j = 0; j < blk; ++j) {
                for (int k = A[id[i+j]]+1; k <= NN; k += k&-k) --BIT[k];
            }
            cnt0 += blk * blk - tmp, cnt1 += tmp;
        }
        // debug(blk, cnt0, cnt1);
        return cnt1 < cnt0;
    };
    
    int64_t min_inv = NN * (NN-1) / 2;
    string min_ans;
    
    for (int op2 = 0; op2 < N; ++op2) {
        // debug(op2);
        
        vector<int> id(NN); iota(ALL(id), 0);
        for (int i = 0; i < op2; ++i) ch2(id);
        
        for (int bit = 0, OLD = calc_inv(id); bit < N; ++bit) {
            for (int &x : id) x ^= (1 << bit);
            int NEW = calc_inv(id);
            if (OLD < NEW) for (int &x : id) x ^= (1 << bit);
            else OLD = NEW;
        }
        chmin(min_inv, calc_inv(id));
        
        // int C = 0;
        // for (int bit = 0; bit < N; ++bit) {
        //     if (calc_cost(id, 1<<bit)) {
        //         C ^= (1 << bit);
        //         for (int &x : id) x ^= (1 << bit);
        //     }
        // }
        // debug(C);
        // for (int i = 0; i < NN; ++i) cerr << id[i] << " \n"[i == NN-1];
        
        // string ans(op2, '2');
        // for (int i = 0; i < N; ++i) {
        //     ans += "2";
        //     if (C >> i & 1) ans += "1";
        // }
        // if (chmin(min_inv, calc_inv(id))) min_ans = ans;
    }
    
    cout << min_inv << "\n";
    cout << min_ans << "\n";
}

/*
2
0 3 1 2

3
2 3 7 6 1 4 5 0

4
1 4 8 5 3 6 12 13 10 11 2 9 14 0 15 7

2
1 0 3 2

3
6 2 7 3 4 0 5 1
*/

int32_t main() {
    fastIO();
    
    int t = 1; // cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        solve();
    }
    
    return 0;
}

#else

#ifdef local
#define _GLIBCXX_DEBUG 1
#endif
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define int int64_t
// #define double __float80
using pii = pair<int, int>;
template <typename T> using Prior = std::priority_queue<T>;
template <typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>;

// #define X first
// #define Y second
#define eb emplace_back
#define ef emplace_front
#define ee emplace
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define SZ(x) ((int)(x).size())

#ifdef local
#define fastIO() void()
#define debug(...) \
    fprintf(stderr, "%sAt [%s], line %d: (%s) = ", "\u001b[33m", __FUNCTION__, __LINE__, #__VA_ARGS__), \
    _do(__VA_ARGS__), fprintf(stderr, "%s", "\u001b[0m")
template <typename T> void _do(T &&_t) {cerr << _t << "\n";}
template <typename T, typename ...U> void _do(T &&_t, U &&..._u) {cerr << _t << ", ", _do(_u...);}
#else
#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define debug(...) void()
#endif

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename T, typename U> bool chmin(T &lhs, U rhs) {return lhs > rhs ? lhs = rhs, 1 : 0;}
template <typename T, typename U> bool chmax(T &lhs, U rhs) {return lhs < rhs ? lhs = rhs, 1 : 0;}

#endif

Compilation message

cheerleaders.cpp: In function 'void solve()':
cheerleaders.cpp:12:10: warning: variable 'ch1' set but not used [-Wunused-but-set-variable]
   12 |     auto ch1 = [&](vector<int> &Z) {
      |          ^~~
cheerleaders.cpp:40:10: warning: variable 'calc_cost' set but not used [-Wunused-but-set-variable]
   40 |     auto calc_cost = [&](vector<int> &id, int blk) -> bool {
      |          ^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Correct!
2 Correct 0 ms 212 KB Correct number of inversions, wrong sequence of operations
3 Correct 0 ms 212 KB Correct number of inversions, wrong sequence of operations
4 Correct 0 ms 212 KB Correct number of inversions, wrong sequence of operations
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Correct number of inversions, wrong sequence of operations
2 Correct 1 ms 212 KB Correct number of inversions, wrong sequence of operations
3 Correct 0 ms 212 KB Correct number of inversions, wrong sequence of operations
4 Correct 1 ms 212 KB Correct number of inversions, wrong sequence of operations
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 340 KB Correct number of inversions, wrong sequence of operations
2 Correct 6 ms 340 KB Correct number of inversions, wrong sequence of operations
3 Correct 6 ms 340 KB Correct number of inversions, wrong sequence of operations
4 Correct 6 ms 340 KB Correct number of inversions, wrong sequence of operations
# 결과 실행 시간 메모리 Grader output
1 Incorrect 141 ms 1608 KB Correct number of inversions, wrong sequence of operations
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1422 ms 5456 KB Correct number of inversions, wrong sequence of operations
2 Correct 1386 ms 6256 KB Correct number of inversions, wrong sequence of operations
3 Correct 1730 ms 6128 KB Correct number of inversions, wrong sequence of operations
4 Correct 1891 ms 6252 KB Correct number of inversions, wrong sequence of operations
5 Correct 1797 ms 6248 KB Correct number of inversions, wrong sequence of operations