Submission #143770

#TimeUsernameProblemLanguageResultExecution timeMemory
143770dacin21Arranging Shoes (IOI19_shoes)C++17
100 / 100
142 ms72308 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using fl = long double;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename S, typename T>
void xmin(S&a, T const&b){if(b<a) a=b;}
template<typename S, typename T>
void xmax(S&a, T const&b){if(b>a) a=b;}

template<bool enabled>
struct Debug{
    template<typename S, typename T = void> struct Tag_Printable : false_type {};
    template<typename S> struct Tag_Printable<S, decltype((void)(cerr << declval<S>()))> : true_type {};
    template<typename S, typename T = void> struct Tag_Iterable: false_type {};
    template<typename S> struct Tag_Iterable<S, decltype((void)(begin(declval<S>()), end(declval<S>())))> : true_type {};
    template<typename T, typename... Args>
    Debug& print(T const&x, true_type, Args...){
        #ifdef LOCAL_RUN
        if(enabled){
            cerr << boolalpha << x;
        }
        #endif // LOCAL_RUN
        return *this;
    }
    template<typename T>
    Debug& print(T const&x, false_type, true_type){
        *this << "[";
        bool first = true;
        for(auto &e:x){
            if(!first) *this << ", ";
            *this << e;
            first = false;
        }
        return *this << "]";
    }
    template<typename S, typename T>
    Debug& print(pair<S, T> const&x, false_type, false_type){
        return *this << "(" << x.first << ", " << x.second << ")";
    }
    template<typename T>
    Debug& operator<<(T const&x){
        return print(x, Tag_Printable<T>{}, Tag_Iterable<T>{});
    }
};
 Debug<true> debug;
// Debug<false> debug; // disable debug printing
#define named(x) string(#x) << " : " <<  x

constexpr int inf = 1e9;

template<typename T>
T iabs(T const&a){
    return max(a, -a);
}

struct Bit{
    Bit(int n_) : n(n_+5), a(n){}

    int q(int x){
        int ret = 0;
        for(++x;x;x-=x&-x){
            ret+=a[x];
        }
        return ret;
    }
    void u(int x, int v){
        for(++x;x<n;x+=x&-x){
            a[x]+=v;
        }
    }

    int n;
    vector<int> a;
};

long long count_swaps(std::vector<int> v) {
    const int n = v.size()/2;
    ll ret = 0;
    vector<deque<int> > occs(n+1);
    vector<pair<int, int> > ps;
    for(int i=0;i<2*n;++i){
        const int a = v[i];
        const int aa = iabs(a);
        if(occs[aa].empty() || v[occs[aa].back()] == a){
            occs[aa].push_back(i);
        } else {
            const int j = occs[aa].front();
            occs[aa].pop_front();
            if(a<0) ps.emplace_back(i, j);
            else ps.emplace_back(j, i);
        }
    }
    assert((int)ps.size() == n);
    sort(ps.begin(), ps.end(), [](auto const&a, auto const&b){return a.first+a.second < b.first+b.second;});
    debug << ps << "\n";
    vector<int> w(2*n);
    for(int i=0;i<n;++i){
        const int l = 2*i;
        auto const&e = ps[i];
        w[l] = e.first;
        w[l+1] = e.second;
    }
    Bit fen(2*n);
    for(int i=0;i<2*n;++i){
        ret+=i-fen.q(w[i]);
        fen.u(w[i], 1);
    }
    ret = ret;
    return ret;
}
#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...