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 "shoes.h"
#include <bits/stdc++.h>
using namespace std;
const int mxN = 2*100005;
int bit[mxN];
bool vis[mxN];
void upd(int pos, int n) {
while(pos <= n) {
bit[pos]++;
pos += (pos&(-pos));
}
}
int ask(int pos) {
int ans = 0;
while(pos > 0) {
ans += bit[pos];
pos -= (pos&(-pos));
}
return ans;
}
long long count_swaps(vector<int> s) {
int n = s.size();
long long ans = 0;
map<int, deque<int> > mp;
for(int i = 0; i < n; i++)
mp[s[i]].push_back(i+1);
for(int i = 0; i < n; i++) {
if(vis[i]) continue;
mp[s[i]].pop_front();
int neg, pos;
if(s[i] > 0) {
pos = i+1;
neg = mp[-s[i]].front();
mp[-s[i]].pop_front();
} else {
neg = i+1;
pos = mp[-s[i]].front();
mp[-s[i]].pop_front();
}
if(neg > pos) {
ans += ((neg-pos)-(ask(neg)-ask(pos-1)));
upd(neg, n);
} else {
ans += ((pos-neg-1)-(ask(pos)-ask(neg-1)));
upd(pos, n);
}
vis[neg-1] = vis[pos-1] = true;
}
return ans;
}
// int main() {
// int n;
// scanf("%d", &n);
// vector<int> s(n);
// for(int i = 0; i < n; i++)
// scanf("%d", &s[i]);
// printf("%lld\n", count_swaps(s));
// return 0;
// }
# | 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... |