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;
#define ll long long
int nr_ofTaken(int c1, int c2, vector<int> &Taken)
{
if (c1 == c2)
return Taken[c1];
return Taken[c1] + nr_ofTaken(c1 + 1, c2, Taken);
}
ll count_swaps (vector<int> S)
{
int n = S.size() / 2;
ll ans = 0;
vector<int> Taken(2 * n, 0);
vector<deque<int>> Poz(2 * n + 2);
for (int i = 0; i < 2 * n; i++)
{
if (S[i] < 0)
Poz[abs(S[i])].push_back(i);
else
Poz[S[i] + n].push_back(i);
}
for (int i = 0; i < 2 * n - 1; i++)
{
if (Taken[i] == 1)
continue;
if (S[i] < 0 && 0 - S[i + 1] == S[i])
{
Taken[i] = 1; Taken[i + 1] = 1;
Poz[abs(S[i])].pop_front();
Poz[abs(S[i]) + n].pop_front();
continue;
}
Taken[i] = 1;
// TREBUIE SA SCADEM SI NR DE TAKEN[J] = 1 UNDE J = i, need
int need;
if (S[i] < 0)
{
Poz[abs(S[i])].pop_front();
need = Poz[abs(S[i]) + n].front();
Poz[abs(S[i]) + n].pop_front();
ans += need - i - 1 - nr_ofTaken(i + 1, need, Taken);
Taken[need] = 1;
}
else
{
Poz[abs(S[i]) + n].pop_front();
need = Poz[abs(S[i])].front();
Poz[abs(S[i])].pop_front();
ans += need - i - nr_ofTaken(i + 1, need, Taken);
Taken[need] = 1;
}
}
return ans;
}
# | 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... |