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;
using ll = long long;
#define sz(x) ((int)x.size())
ll subtask3(vector<int> S) {
int n = (int)S.size();
ll ans = 0;
ll L = 0, R = 0;
for(int i = 0; i < n; ++i) {
if(S[i] > 0) {
if(L > 0) {
L--;
ans += L;
}
else R++;
}
else {
if(R > 0) {
ans += R;
R--;
}
else L++;
}
}
return ans;
}
ll subtask4(vector<int> S) {
int n = (int)S.size();
ll ans = 0;
for(int i = 0; i < n/2; ++i) {
ans += (ll)i;
}
return ans;
}
ll subtask5(vector<int> S) {
int n = (int)S.size();
ll ans = 0;
int i = 0;
while(i < n) {
if(S[i] < 0) {
int j = -1;
for(int k = i + 1; k < n; ++k) if(S[k] == -S[i]) {
j = k;
break;
}
for(int l = j; l > i + 1; --l)
swap(S[l], S[l-1]), ++ans;
}
else {
if(i > 0 && S[i-1] == -S[i]) {
++i;
continue;
}
int j = -1;
for(int k = i + 1; k < n; ++k) if(S[k] == -S[i]) {
j = k;
break;
}
for(int l = j; l > i; --l)
swap(S[l], S[l-1]), ++ans;
}
i++;
}
return ans;
}
ll count_swaps(vector<int> S) {
int n = (int)S.size();
ll ans = 0;
vector<bool> prise(n, false);
bool sub3 = true;
for(int i : S) if(abs(i) != abs(S[0])) sub3 = false;
if(sub3) return subtask3(S);
bool sub4 = true;
for(int i = 0; i < n / 2; ++i)
if(S[i] > 0 || S[i + n/2] < 0 || -S[i] != S[i + n/2])
sub4 = false;
if(sub4) return subtask4(S);
//if(n <= 1000) return subtask5(S);
vector<deque<int>> L(n+1), R(n+1);
vector<ll> avant(n+1,0);
for(int i = 0; i < n; ++i)
avant[i] = n - i - 1;
for(int i = 0; i < n; ++i) {
int x = abs(S[i]);
if(S[i] > 0) {
if(sz(L[x]) > 0) {
int j = L[x].front();
ans += avant[j] - avant[i] - 1LL;
for(int k = j + 1; k <= i; ++k)
avant[k]--;
L[x].pop_front();
}
else R[x].push_back(i);
}
else {
if(sz(R[x]) > 0) {
int j = R[x].front();
ans += avant[j] - avant[i];
for(int k = j; k <= i; ++k)
avant[k]--;
R[x].pop_front();
}
else L[x].push_back(i);
}
}
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... |