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>
#include "shoes.h"
using namespace std;
const int N = 1e5 + 5;
struct fenw {
int t[N] = {0};
int query(int i) {
int ans = 0;
for(; i > 0; i -= i & - i) {
ans += t[i];
}
return ans;
}
void update(int i, int v) {
for(; i < N; i += i & - i) {
t[i] += v;
}
}
}fenw;
long long count_swaps(std::vector<int> s) {
int n = s.size();
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
a[i] = s[i - 1];
}
map<int,int> M, M1;
for(int i = 1; i <= n; i++) {
if(a[i] > 0) {
M[a[i]] += 1;
}
else {
M1[a[i]] += 1;
}
}
for(int i = 1; i <= n; i++) {
if(a[i] > 0) {
if(M[a[i]] != M1[-a[i]]) {
return - 1;
}
}
}
map<int,set<int>> ms;
for(int i = 1; i <= n; i++) {
ms[a[i]].insert(i);
}
vector<int> izracunao(n + 1, 0);
int ans = 0;
for(int i = 1; i <= n; i++) {
if(izracunao[i]) {
continue;
}
auto it = ms[-a[i]].upper_bound(i);
if(it == ms[-a[i]].end()) {
continue;
}
int idx = *it;
if(a[i] > 0) {
//cout << 1 << " " << i << " " << idx << "\n";
ans += (idx + fenw.query(idx)) - (i + fenw.query(i));
fenw.update(i, 1);
fenw.update(idx, - 1);
}
else {
//cout << 2 << " " << i << " " << idx << "\n";
ans += (idx + fenw.query(idx)) - (i + fenw.query(i)) - 1;
fenw.update(i, 1);
fenw.update(idx, - 1);
}
izracunao[idx] = 1;
}
return ans;
}
/*
int main() {
cout << count_swaps({- 2, 2, 2, - 2, - 2, 2});
}*/
# | 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... |