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;
const int MAXN = 1e5 + 5;
typedef long long ll;
int n;
int inv(int x){
if(x<0) return -x;
else if(x>n) return x - n;
else return x + n;
}
ll count_swaps(vector<int> S){
ll ans1 = 0, ans2 = 0;
n = S.size() / 2;
queue<int> mp[2 * n + 2];
int nxt = 0;
for(int i=0; i<2*n; i++) {
if(mp[inv(S[i])].size()){
ans1 += abs(mp[inv(S[i])].front() - nxt) + abs(i - nxt - 1);
ans2 += (S[i]<0);
nxt += 2;
mp[inv(S[i])].pop();
}
else{
mp[inv(inv(S[i]))].push(i);
}
}
for(int i=1; i<=2*n; i++){
while(mp[i].size()){
mp[i].pop();
}
}
ll ans3 = 0, ans4 = 0; nxt = 0;
reverse(S.begin(), S.end());
for(int i=0; i<2*n; i++) {
if(mp[inv(S[i])].size()){
ans3 += abs(mp[inv(S[i])].front() - nxt) + abs(i - nxt - 1);
ans4 += (S[i]<0);
nxt += 2;
mp[inv(S[i])].pop();
}
else{
mp[inv(inv(S[i]))].push(i);
}
}
return min(ans1 / 2 + ans2, ans3 / 2 + ans4);
}
# | 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... |