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
#define ll long long
using namespace std;
const int N = 100005;
vector <int> lf[N],rg[N];
bool f[N];
ll fen[N];
pair<int,pair<int,int> > d[N];
void upd(int ind,int val){
while(ind <= N){
fen[ind] += val;
ind += (ind&(-ind));
}
}
ll get(int ind){
ll s=0;
while(ind){
s += fen[ind];
ind -= (ind&(-ind));
}
return s;
}
ll 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);
}
ll 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);
ll 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 + get(r + 1) - get(l);
if (a[l] > 0 && a[r] < 0) ans++;
upd(l + 1, -1);
upd(r + 1, -1);
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 0; i < a.size(); i++){
| ~~^~~~~~~~~~
shoes.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | 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... |