제출 #848603

#제출 시각아이디문제언어결과실행 시간메모리
848603anha3k25cvpArranging Shoes (IOI19_shoes)C++14
100 / 100
123 ms140396 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
#include "shoes.h"

using namespace std;

const int N = 5 + 1e5;
const int inf = 7 + 1e9;

struct Fenwick {
    int n;
    vector <int> tree;
    Fenwick (int _n = 0) : n(_n) {
        tree.assign(n + 1, 0);
    }
    void update(int x, int val) {
        for (int i = x; i <= n; i += i & -i)
            tree[i] += val;
    }
    int get(int x) {
        int ans = 0;
        for (int i = x; i > 0; i -= i & -i)
            ans += tree[i];
        return ans;
    }
};

ll count_swaps(vector<int> a) {
	int n = (int)a.size() / 2;
	vector <queue <int>> q(2 * n + 2);
	for (int i = 0; i < 2 * n; i ++)
        q[a[i] + n + 1].push(i + 1);
    Fenwick f(2 * n + 1);
    for (int i = 1; i <= 2 * n + 1; i ++)
        f.update(i, 1);
    vector <int> vis(2 * n, 1);
    int pos = 0;
    ll ans = 0;
    for (int time = 0; time < n; time ++) {
        while (!vis[pos])
            pos ++;
        int x = a[pos] + n + 1, y = n + 1 - a[pos], u = q[y].front();
        q[x].pop(); q[y].pop();
        f.update(pos + 1, -1);
        ans += f.get(u) - 1 + (a[pos] > 0);
        f.update(u, -1);
        vis[pos] = 0;
        vis[u - 1] = 0;
    }
	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...