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 <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#define pb push_back
using namespace std;
const int N = 100005;
vector <int> lf[N],rg[N];
bool f[N];
pair<int,pair<int,int> > d[N];
long long count_swaps(vector <int> a) {
for (int i = 0; i < a.size(); i++){
if (a[i] < 0) lf[-a[i]].pb(i);
else rg[a[i]].pb(i);
}
int cnt = 0;
for (int i = 0; i < a.size(); i++){
if (f[i]) continue;
if (a[i] < 0){
int ind = rg[-a[i]][0];
f[ind] = 1;
rg[-a[i]].erase(rg[-a[i]].begin());
d[++cnt] = {abs(ind - i),{i,ind}};
lf[-a[i]].erase(lf[-a[i]].begin());
}
else{
int ind = lf[a[i]][0];
f[ind] = 1;
lf[a[i]].erase(lf[a[i]].begin());
d[++cnt] = {abs(ind - i),{ind,i}};
rg[a[i]].erase(rg[a[i]].begin());
}
}
sort(d + 1,d + cnt + 1);
int ans=0;
for (int i = 1; i <= cnt; i++){
int l = d[i].second.first,r = d[i].second.second;
if (l > r) swap(l,r);
ans += r - l - 1;
if (a[l] > 0 && a[r] < 0) ans++;
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:15:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for (int i = 0; i < a.size(); i++){
| ~~^~~~~~~~~~
shoes.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < a.size(); i++){
| ~~^~~~~~~~~~
# | 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... |