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;
long long count_swaps(std::vector<int> s) {
int n = s.size();
map<int,int> M, M1;
vector<int> a = s;
for(int i = 0 ; i < n; i++) {
if(a[i] > 0) {
M[a[i]] += 1;
}
else {
M1[a[i]] += 1;
}
}
int ok = 0;
for(int i = 0; i < n; i++) {
if(a[i] > 0) {
if(M[a[i]] != M1[-a[i]]) {
ok = 1;
}
}
}
if(ok) {
return - 1;
}
vector<int> izracunao(n, 0);
int ans = 0;
for(int i = 0; i < n; i++) {
if(izracunao[i]) {
continue;
}
if(a[i] > 0) {
int idx = - 1;
for(int j = i + 1; j < n; j++) {
if(izracunao[j]) {
continue;
}
if(a[i] == - a[j]) {
idx = j;
break;
}
}
ans += idx - i;
izracunao[idx] = 1;
//cout << "1 :::" << i << " " << idx << "\n";
}
else {
int idx = - 1;
for(int j = i; j < n; j++) {
if(izracunao[j]) {
continue;
}
if(a[i] == - a[j]) {
idx = j;
break;
}
}
ans += idx - i - 1;
izracunao[idx] = 1;
//cout << "2 :::" << i << " " << idx << "\n";
}
}
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... |