Submission #201481

#TimeUsernameProblemLanguageResultExecution timeMemory
201481combi1k1Arranging Shoes (IOI19_shoes)C++14
45 / 100
176 ms139904 KiB
#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define ld  double

#define sz(x)   (int)x.size()
#define all(x)  x.begin(),x.end()

#define pb  emplace_back
#define X   first
#define Y   second

const int   N   = 2e5 + 5;

typedef pair<int,int>   ii;

int t[N];

int upd(int p)  {
    for(; p > 0 ; p -= p & -p)
        t[p]++;
    return  1;
}
int get(int p)  {
    int ans = 0;
    for(; p < N ; p += p & -p)
        ans += t[p];
    return  ans;
}

queue<int> pos[N];

ll  count_swaps(vector<int> S)  {
    queue<ii> quL;

    int n = S.size() / 2;

    for(int i = 1 ; i <= n + n ; ++i)   {
        int x = S[i - 1];
        if (x < 0)  quL.push(ii(-x,i));
        if (x > 0)  pos[x].push(i);
    }
    ll  ans = 0;

    for(int i = 1 ; i <= n + n ; ++i)   {
        int s = quL.front().X;
        int p = quL.front().Y;  quL.pop();

        ans += p + get(p) - i++;    upd(p); p = pos[s].front(); pos[s].pop();
        ans += p + get(p) - i;      upd(p);
    }
    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...