Submission #298295

#TimeUsernameProblemLanguageResultExecution timeMemory
298295emil_physmathArranging Shoes (IOI19_shoes)C++17
0 / 100
5 ms4992 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
using llong = long long;
#define BUGO(x) cerr << #x << " = " << x << '\n';
const int maxN = 200'005;

int t[maxN * 4];

int Get(int v, int vl, int vr, int i) {
    if (vl == vr)
        return t[v];
    int vm = vl + (vr - vl) / 2;
    if (i <= vm)
        return t[v] + Get(v * 2, vl, vm, i);
    return t[v] + Get(v * 2 + 1, vm + 1, vr, i);
}
void Add(int v, int vl, int vr, int l, int r, int val) {
    if (l > vr || vl > r) return;
    if (l <= vl && vr <= r) {
        t[v] += val;
        return;
    }
    int vm = vl + (vr - vl) / 2;
    Add(v * 2, vl, vm, l, r, val);
    Add(v * 2 + 1, vm + 1, vr, l, r, val);
}
vector<int> pos_[maxN];
vector<int>* pos = pos_ + maxN;
long long count_swaps(vector<int> a) {
    int n = a.size();
    for (int i = 0; i < n; ++i)
        pos[a[i]].push_back(i);
    for (int i = -n; i <= n; ++i)
        reverse(pos[i].begin(), pos[i].end());
    llong ans = 0;
    for (int i = 0; i < n; i += 2)
    {
        if (abs(a[i]) == abs(a[i + 1]) && a[i] < 0 && a[i + 1] > 0) {
            pos[a[i]].pop_back();
            pos[a[i + 1]].pop_back();
            continue;
        }
        int j = pos[-a[i]].back();
        pos[-a[i]].pop_back();
        pos[a[i]].pop_back();

        ans += (Get(1, 0, n - 1, j) - (i + 1));
        Add(1, 0, n - 1, i + 1, j, 1);
        // while (j > i + 1) {
            // ++ans;
            // swap(a[j - 1], a[j]);
            // --j;
        // }
        if (a[i] > 0) {
            // swap(a[i], a[i + 1]);
            ++ans;
        }
    }
    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...