제출 #289902

#제출 시각아이디문제언어결과실행 시간메모리
289902evpipisArranging Shoes (IOI19_shoes)C++14
100 / 100
113 ms21248 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
typedef long long ll;
const int len = 2e5+5, mx = 2e5;
int vis[len], tree[len], shoe[len];
vector<int> lef[len], rig[len];

void upd(int i, int v){
    while (i <= mx)
        tree[i] += v, i += i&(-i);
}

int ask(int i){
    int ans = 0;
    while (i > 0)
        ans += tree[i], i -= i&(-i);
    return ans;
}

ll count_swaps(vector<int> S) {
    ll ans = 0;
    int n = S.size();

    for (int i = 1; i <= n; i++)
        shoe[i] = S[i-1];

    for (int i = n; i >= 1; i--){
        if (shoe[i] > 0) rig[shoe[i]].pb(i);
        else lef[-shoe[i]].pb(i);

        upd(i, +1);
    }

    for (int i = 1; i <= n; i++){
        if (vis[i]) continue;

        int j = lef[abs(shoe[i])].back();
        if (shoe[i] < 0)
            j = rig[abs(shoe[i])].back();

        ans += ask(j)-2 + (shoe[i] > 0);

        vis[i] = vis[j] = 1;
        upd(i, -1), upd(j, -1);
        lef[abs(shoe[i])].pop_back();
        rig[abs(shoe[i])].pop_back();
    }

	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...