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 = 2e5 + 5;
#define pii pair<int, int>
#define ff first
#define ss second
int64_t count_swaps(vector<int> S){
int n = S.size();
pii mp[maxn];
int pref[maxn];
for(int i = 0; i < n; i++){
int val = abs(S[i]);
if(mp[val].ff) mp[val].ss = i, pref[i] = pref[i-(i>0)]-1;
else mp[val].ff = i, pref[i] = pref[i-(i>0)]+1;
}
int64_t ans = 0;
for(int i = 0; i < n; i++){
int val = abs(S[i]);
int mn = min(mp[val].ff, mp[val].ss);
int mx = max(mp[val].ff, mp[val].ss);
if(i==mn){
ans+= mx-mn-1 + (S[i]<0) - pref[mx];
}
}
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... |