# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
690967 | finn__ | Arranging Shoes (IOI19_shoes) | C++17 | 81 ms | 16548 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include "shoes.h"
struct FenwickTree
{
vector<int> t;
FenwickTree(size_t n)
{
t = vector<int>(n, 0);
}
void prefix_increment(int i, int x)
{
i++;
while (i <= t.size())
{
t[i - 1] += x;
i += i & -i;
}
}
void range_increment(int i, int j, int x)
{
prefix_increment(i, x);
if (j < t.size() - 1)
prefix_increment(j + 1, -x);
}
int point_query(int i)
{
i++;
int x = 0;
while (i)
{
x += t[i - 1];
i -= i & -i;
}
return x;
}
};
long long count_swaps(vector<int> s)
{
size_t const n = s.size();
vector<bool> processed(n, 0);
vector<priority_queue<unsigned, vector<unsigned>, greater<unsigned>>> q(n);
for (unsigned i = 0; i < n; i++)
q[2 * (abs(s[i]) - 1) + (s[i] < 0)].push(i);
long long swaps_needed = 0;
FenwickTree tree(n);
for (unsigned i = 0; i < n; i++)
{
if (!processed[i])
{
processed[i] = 1;
while (processed[q[2 * (abs(s[i]) - 1) + (s[i] > 0)].top()])
q[2 * (abs(s[i]) - 1) + (s[i] > 0)].pop();
unsigned j = q[2 * (abs(s[i]) - 1) + (s[i] > 0)].top();
q[2 * (abs(s[i]) - 1) + (s[i] > 0)].pop();
processed[j] = 1;
swaps_needed += j + tree.point_query(j) - i - tree.point_query(i) - (s[i] < 0);
tree.range_increment(i, j, 1);
}
}
return swaps_needed;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |