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>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
typedef pair<int,int> pii;
int n;
ll bit[maxn];
inline ll sum(int x){
ll res = 0;
while(x>0){
res += bit[x];
x -= (x&-x);
}
return res;
}
inline void upd(int x, ll v){
while(x<maxn){
bit[x] += v;
x += (x&-x);
}
}
deque<int> d[maxn];
ll count_swaps(vector<int> s){
n = s.size();
ll ans = 0LL;
vector<pii> l, r;
for(int i=0; i<n; i++){
if(!d[abs(s[i])].empty() and d[abs(s[i])].back() != s[i]){
d[abs(s[i])].pop_back();
r.push_back({abs(s[i]), i});
if(s[i] < 0) ans++;
}
else{
d[abs(s[i])].push_front(s[i]);
l.push_back({abs(s[i]), i});
}
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
for(int i=0; i<l.size(); i++){
r[i].first = l[i].second;
l[i].first = l[i].second;
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
int tt = 0;
for(int i=0; i<l.size(); i++){
s[l[i].second] = ++tt;
s[r[i].second] = ++tt;
}
for(int i=0; i<n; i++){
upd(s[i], 1LL);
ans += sum(n) - sum(s[i]);
}
return ans;
}
/*int main(){
int n;
cin >> n;
vector<int> s;
while(n--){
int x;
cin >> x;
s.push_back(x);
}
cout << count_swaps(s) << endl;
}*/
Compilation message (stderr)
shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:53:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<l.size(); i++){
~^~~~~~~~~
shoes.cpp:63:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<l.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... |