# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
944806 | PenguinsAreCute | Arranging Shoes (IOI19_shoes) | C++17 | 68 ms | 16656 KiB |
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;
using ll = long long;
struct ft {
vector<int> v; int n;
ft(int _n): n(_n) {v.resize(n+1,0);}
inline int lsb(int x) {return x&(-x);}
void up(int x, int u) {
for(;x<=n;x+=lsb(x)) v[x]+=u;
}
int qry(int x) {
ll ans = 0;
for(;x;x-=lsb(x)) ans += v[x];
return ans;
}
};
long long count_swaps(std::vector<int> s) {
int n = s.size() / 2;
vector<int> v, posP[n+1], posN[n+1];
for(int i=0;i<2*n;i++) {
if(s[i]<0) posN[-s[i]].push_back(i);
else posP[s[i]].push_back(i);
}
vector<pair<int,int>> vec;
for(int i=1;i<=n;i++) for(int j=0;j<posN[i].size();j++) vec.push_back({posN[i][j],posP[i][j]});
sort(vec.begin(),vec.end(),[](pair<int,int> a, pair<int,int> b) {return a.first+a.second<b.first+b.second;});
v.resize(2*n);
for(int i=0;i<2*n;i+=2) {
v[i] = vec[i>>1].first;
v[i+1] = vec[i>>1].second;
}
ll ans = 0; ft fenwick(2*n);
for(int i=2*n;i--;) {
ans += fenwick.qry(v[i]);
fenwick.up(v[i]+1,1);
}
return ans;
}
Compilation message (stderr)
# | 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... |