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;
int N;
void change(int index, int delta, int BIT[]){
for(; index <N; index += (index&(-index))){
BIT[index]+=delta;
}
}
int query(int index, int BIT[]){
int ans = 0;
for(; index > 0; index -= (index&(-index))) ans+= BIT[index];
return ans;
}
long long count_swaps(vector<int> s) {
map<int, queue<int>> M;
N = s.size()+1;
int BIT[N];
memset(BIT, 0, sizeof(BIT));
for(int i = 1; i < N; ++i) change(i, 1, BIT);
long long ans = 0;
for(int i = 0; i < s.size(); ++i){
//cout << i << " " << ans << endl;
if(M.find(-s[i]) == M.end()){
if(M.find(s[i]) == M.end()){
M[s[i]] = queue<int>({i});
}
else if(M[s[i]].empty()){
M[s[i]] = queue<int>({i});
}
else{
M[s[i]].push(i);
}
}
else if(M[-s[i]].empty()){
if(M.find(s[i]) == M.end()){
M[s[i]] = queue<int>({i});
}
else if(M[s[i]].empty()){
M[s[i]] = queue<int>({i});
}
else{
M[s[i]].push(i);
}
}
else{
if(s[i] < 0) ans++;
//cout << "a ";
int prev = M[-s[i]].front();
//cout <<"b ";
M[-s[i]].pop();
//cout <<"c ";
ans += (long long)query(i, BIT)-(long long)query(prev+1, BIT);
//cout <<"d ";
change(i+1, -1, BIT);
//cout << "e ";
change(prev+1, 1, BIT);
}
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:27:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i = 0; i < s.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... |