Submission #408774

#TimeUsernameProblemLanguageResultExecution timeMemory
408774dxz05Arranging Shoes (IOI19_shoes)C++14
10 / 100
196 ms273620 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 4e5 + 6e3;

int f[MAXN];

void add(int x, int y){
    x++;
    while (x < MAXN){
        f[x] += y;
        x += (-x & x);
    }
}

void add(int l, int r, int y){
    if (l > r) return;
    add(l, y);
    add(r + 1, -y);
}

int get(int x){
    x++;
    int sum = 0;
    while (x > 0){
        sum += f[x];
        x -= (-x & x);
    }
    return sum;
}

int get(int l, int r){
    return get(r) - get(l - 1);
}

int code(int val){
    return (val < 0 ? -val : val + 200000);
}

vector<pair<int, int>> added_segments;

int original_position(int x){
    for (auto now : added_segments){
        if (now.first < x && x < now.second) x++;
    }
    return x;
}

queue<int> q[MAXN];

long long count_swaps(vector<int> a) {
	int n = a.size();

	for (int i = 0; i < n; i++){
        q[code(a[i])].push(i);
	}

    vector<bool> used(n, false);

    ll ans = 0;

    for (int i = 0; i < n; i++){
        if (used[i]) continue;

        int x = code(a[i]), y = code(-a[i]);

        int pos1 = i, pos2 = q[y].front();
        added_segments.emplace_back(pos1, pos2);
        used[pos2] = true;

        pos1 = original_position(pos1), pos2 = original_position(pos2);

        //cout << pos1 << ' ' << pos2 << endl;

        ans += pos2 - pos1 - 1 + (a[i] > 0);

        q[x].pop();
        q[y].pop();
    }

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