제출 #1327748

#제출 시각아이디문제언어결과실행 시간메모리
1327748poapaa.Arranging Shoes (IOI19_shoes)C++20
100 / 100
309 ms148336 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200'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 + 1; i <= n; i += i & ( -i ) )
        aib[i] += val;
}

ll query( int pos ) {
    ll s = 0;
    for( int i = pos + 1; 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 = 0; i < n; i++ )
        update( i, 1 );

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