Submission #298838

#TimeUsernameProblemLanguageResultExecution timeMemory
298838Aldas25Arranging Shoes (IOI19_shoes)C++14
100 / 100
661 ms148984 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;

const int MAXN = 500100;

ll fen[MAXN];

void upd (int p, ll x) {
    p++;
    for (int i = p; i < MAXN; i += i&(-i))
        fen[i] += x;
}
ll get (int p) {
    p++;
    ll ret = 0ll;
    for (int i = p; i > 0; i -= i&(-i))
        ret += fen[i];
    return ret;
}
ll get (int le, int ri) {
    return get(ri) - get(le-1);
}

map<int, queue<int>> q;

long long count_swaps(std::vector<int> s) {
    int n = (int)s.size();

    FOR(i, 0, n-1) upd(i, 1);
    FOR(i, 0, n-1) q[s[i]].push(i);

    ll ret = 0;
    FOR(i, 0, n-1) {
        if (get(i,i) == 0) continue;
        q[s[i]].pop();
        int id = q[-s[i]].front();
        q[-s[i]].pop();
        if (s[i] > 0) ret++;
        upd (i, -1);
        upd (id, -1);
        ret += get(i, id);
    }

	return ret;
}

/*

2
2 1 -1 -2
ans:
4

3
-2 2 2 -2 -2 2
ans:
1


*/
#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...