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;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> plll;
typedef pair<pll,pll> ppll;
typedef long double ld;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fst first
#define snd second
#define ins insert
#define pb push_back
template< typename T,typename V>ostream &operator<< (ostream &out,const pair<T,V> x){ out << "{" << x.fst << " : " << x.snd << "}"; return out;}template< typename T>ostream &operator<< (ostream &out,const set<T> x){ for(auto &it : x){ out << it << " "; } return out;}template< typename T>ostream &operator<< (ostream &out,const multiset<T> x){ for(auto &it : x){ out << it << " "; } return out;}template< typename T,typename V>ostream &operator<< (ostream &out,const map<T,V> x){ for(auto &it : x){ out << "[" << it.fst << "]" << " = " << it.snd << "\n"; } return out;}template< typename T>ostream &operator<< (ostream &out,const vector<T> x){ for(int i = 0;i < x.size() - 1; ++i){ out << x[i] << " "; } out << x.back(); return out;}template< typename T>ostream &operator<< (ostream &out,const vector<vector<T> > x){ for(int i = 0;i < x.size() - 1; ++i){ out << "[" << i << "]" << " = {" << x[i] << "}\n"; } out << "[" << x.size() - 1 << "]" << " = {" << x.back() << "}\n"; return out;}
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9;
set<int> pos1[N],pos2[N];
ll f[N];
void upd(int x){
for(int i = x;i < N; i |= (i + 1)){
f[i]++;
}
}
ll get(int x){
ll ret = 0;
for(int i = x;i >= 0;i = (i & (i + 1)) - 1){
ret += f[i];
}
return ret;
}
ll get(int l,int r){
return get(r) - get(l - 1);
}
bool used[N];
long long count_swaps(std::vector<int> s) {
ll ret = 0;
for(int i = 0;i < s.size(); ++i){
if(s[i] < 0)pos1[-s[i]].ins(i);
else pos2[s[i]].ins(i);
}
for(int i = 0; i < s.size(); ++i){
if(used[i])continue;
int r = 0;
if(s[i] < 0){
r = *pos2[-s[i]].begin();
}else{
r = *pos1[s[i]].begin();
}
if(s[i] > 0)ret++;
if(s[i] < 0)s[i] = -s[i];
pos1[s[i]].erase(pos1[s[i]].begin());
pos2[s[i]].erase(pos2[s[i]].begin());
ret += r - i - 1;
ret -= get(i,r);
used[r] = true;
upd(r);
}
return ret;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:41:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < s.size(); ++i){
~~^~~~~~~~~~
shoes.cpp:45:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
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... |