제출 #724294

#제출 시각아이디문제언어결과실행 시간메모리
724294PixelCatArranging Shoes (IOI19_shoes)C++14
45 / 100
66 ms21548 KiB
#include "shoes.h"
#ifdef NYAOWO
#include "grader.cpp"
#endif

#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define eb emplace_back
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define int LL
using namespace std;
using LL = long long;
using pii = pair<int, int>;

const int MAXN = 200010;

#define LO(x) (x & (-x))
struct BIT {
    int n;
    int a[MAXN];
    void init(int _n) {
        n = _n;
        memset(a, 0, sizeof(a));
    }
    void add(int i, int x) {
        while(i <= n) {
            a[i] += x;
            i += LO(i);
        }
    }
    int ask(int i) {
        int res = 0;
        while(i) {
            res += a[i];
            i -= LO(i);
        }
        return res;
    }
} bit;

vector<int> l[MAXN];
vector<int> r[MAXN];

int solve1(vector<int32_t> s) {
    int n = sz(s) / 2;
    For(i, 1, n) {
        l[i].clear();
        r[i].clear();
    }
    For(i, 0, n * 2 - 1) {
        if(s[i] < 0) l[-s[i]].eb(i + 1);
        else r[s[i]].eb(i + 1);
    }
    vector<pii> owo;
    int add = 0;
    For(siz, 1, n) {
        For(i, 0, sz(l[siz]) - 1) {
            int a = l[siz][i];
            int b = r[siz][i];
            if(a > b) {
                add++;
                swap(a, b);
            }
            owo.eb(a, b);
        }
    }
    // int res = 10000;
    // sort(all(owo));
    // do {
    //     int cur = 0;
    //     int re = 0;
    //     for(auto &p:owo) {
    //         re += abs(p.F - cur) + abs(p.S - cur - 1);
    //         cur += 2;
    //     }
    //     res = min(res, re / 2 + add);
    // } while(next_permutation(all(owo)));
    // return res;
    sort(all(owo), [&](pii a, pii b) {
        if(a.F < b.F && a.S < b.S) return true;
        return false;
    });
    // int cur = 0;
    int res = 0;
    bit.init(n * 2);
    For(i, 1, n * 2) bit.add(i, 1);
    for(auto &p:owo) {
        bit.add(p.F, -1);
        res += bit.ask(p.F);
        bit.add(p.S, -1);
        res += bit.ask(p.S);
        // res += abs(p.F - cur) + abs(p.S - cur - 1);
        // cur += 2;
    }
    return res + add;
    // return res / 2 + add;
}

long long count_swaps(std::vector<int32_t> s) {
    // int n = sz(s) / 2;
    // assert(n <= 8);
    // vector<int32_t> oao = {-2, -1, 1, 2};
    // do {
    //     For(i, 0, 3) cout << oao[i] << " ";
    //     cout << "-> " << solve1(oao) << "\n";
    // } while(next_permutation(all(oao)));
    return solve1(s);
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...