Submission #1327745

#TimeUsernameProblemLanguageResultExecution timeMemory
1327745poapaa.Arranging Shoes (IOI19_shoes)C++20
0 / 100
1096 ms332 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100'000;
using ll = long long;

int aib[MAXN + 1];
int n;

map<int, queue<int>> pos;

int target[MAXN + 1];
int viz[MAXN + 1];

void update( int pos, int val ) {
    for( int i = pos; i <= n; i += i & ( -i ) )
        aib[i] += val;
}

ll query( int pos ) {
    ll s = 0;
    for( int i = pos; i > 0; i -= i & ( -i ) )
        s += aib[i];
    return s;
}

long long count_swaps( vector<int> v ) {
    int i, current;
    ll scor;
    n = v.size();
    for( i = 0; i < n; i++ )
        pos[v[i]].push(i);

    current = 0;
    for( i = 0; i < n; i++ ) {
        if( !viz[i] ) {
            int shoe_size = v[i];
            int pair_size = -v[i];

            int pos1 = i;
            int pos2 = pos[pair_size].front();

            pos[shoe_size].pop();
            pos[pair_size].pop();

            viz[pos1] = viz[pos2] = 1;

            if( shoe_size < 0 ) {
                target[pos1] = current++;
                target[pos2] = current++;
            } else {
                target[pos2] = current++;
                target[pos1] = current++;
            }
        }
    }

    for( i = 1; i <= n; i++ )
        aib[i] = 1;

    scor = 0;
    for( i = 0; i < n; i++ ) {
        scor += query( target[i] );
        update( target[i], -1 );
    }
    return scor;
}
#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...