Submission #404516

#TimeUsernameProblemLanguageResultExecution timeMemory
404516Theo830Arranging Shoes (IOI19_shoes)C++17
100 / 100
119 ms15648 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll INF = 1e9+7;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "shoes.h"
ll bit[300005] = {0};
ll N = 300004;
void upd(ll k,ll x){
    while(k <= N){
        bit[k] += x;
        k += k & -k;
    }
}
ll sum(ll k){
    ll ans = 0;
    while(k >= 1){
        ans += bit[k];
        k -= k & - k;
    }
    return ans;
}
long long count_swaps(std::vector<int> s){
    ll n = s.size() / 2;
    ll ans = 0;
    vll exo[n+5][2];
    ll p = 0;
    for(auto x:s){
        exo[abs(x)][x > 0].pb(p);
        p++;
    }
    f(i,0,n+5){
        reverse(all(exo[i][0]));
        reverse(all(exo[i][1]));
    }
    ll cur = 0;
    f(i,0,n){
        while(sum(cur+1) == cur+1){
            cur++;
        }
        bool e = s[cur] > 0;
        ll pos = exo[abs(s[cur])][e ^ 1].back();
        exo[abs(s[cur])][e ^ 1].pop_back();
        exo[abs(s[cur])][e].pop_back();
        ll ppos = pos,pcur = cur;
        pos += sum(2*n+5) - sum(pos+1);
        cur += sum(2*n+5) - sum(cur+1);
        ans += pos - cur - 1;
        ans += e;
        pos = ppos;
        cur = pcur;
        upd(cur+1,1);
        upd(pos+1,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...