Submission #471021

#TimeUsernameProblemLanguageResultExecution timeMemory
471021jalsolArranging Shoes (IOI19_shoes)C++17
100 / 100
82 ms10576 KiB
// looking to shine brighter in this world...

#define TASK "shoes"
#include "shoes.h"

#ifdef LOCAL
#include "local.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#define DB(...)
#endif

using namespace std;

using ll = long long;
using ld = long double;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
#define Fe(...) for (auto __VA_ARGS__)

template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); }
template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; }
template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; }

const bool __initialization = []() {
    cin.tie(nullptr)->sync_with_stdio(false);
    if (fopen(TASK".inp", "r")) {
        (void)(!freopen(TASK".inp", "r", stdin));
        (void)(!freopen(TASK".out", "w", stdout)); }
    cout << setprecision(12) << fixed;
    return true;
}();

constexpr ld eps = 1e-9;
constexpr int inf = 1e9;
constexpr ll linf = 1e18;

// ================================================================================

constexpr int maxn = 3e5 + 5;

template <class T> struct Fen {
    int n; std::vector<T> fen;

    Fen() : n(0) {}
    Fen(int _n) : n(_n) { fen.assign(_n, 0); }

    void update(int pos, T x) { for (; pos < n; pos |= pos + 1) fen[pos] += x; }
    T query(int pos) { T ret = 0; for (; pos > 0; pos &= pos - 1) ret += fen[pos - 1]; return ret; }
    T query(int l, int r) { return query(r) - query(l); }
};

ll count_swaps(std::vector<int> a) {
    int n = isz(a) / 2;
    vector<vector<pair<int, int>>> idx(n + 1);

    Rep (i, 2 * n) {
        idx[abs(a[i])].emplace_back(a[i], i);
    }

    ll ans = 0;

    Fen<int> fen(2 * n + 5);
    Rep (i, 2 * n) fen.update(i, 1);

    vector<pair<int, int>> pos;

    For (i, 1, n) {
        auto& v = idx[i];

        // negatives first half, positive second half
        sort(all(v));
        int half = isz(v) / 2;

        Rep (j, half) {
            // closest pair of left and right shoes
            int l = v[j].second;
            int r = v[j + half].second;

            // swapping until adjacent,
            // then swap one extra time to switch the left and right
            if (l > r) {
                swap(l, r);
                ++ans;
            }

            pos.emplace_back(l, r);
        }
    }

    sort(all(pos));

    // swapping until l is adjacent to r
    // requires sum in range [l + 1, r)
    Fe (&[l, r] : pos) {
        ans += fen.query(l + 1, r);
        fen.update(l, -1);
        fen.update(r, -1);
    }

    return ans;
}
#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...