# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
797494 | Liudas | Arranging Shoes (IOI19_shoes) | C++17 | 665 ms | 157324 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 <bits/stdc++.h>
using namespace std;
struct fenwick_tree{
vector<int> tree;
fenwick_tree(int N){
tree.assign(N + 5, 0);
for(int i = 1; i <= N; i ++){
for(int j = i; j < tree.size(); j += (j & -j)){
tree[j] ++;
}
}
}
void rem(int p){
for(int i = p; i < tree.size(); i += (i & -i)){
tree[i]--;
}
}
int sum(int p){
int s = 0;
for(int i = p; i > 0; i -= (i & -i)){
s += tree[i];
}
return s;
}
};
long long count_swaps(vector<int> S){
long long N = S.size();
fenwick_tree tree(N);
map<int, queue<int>> arr;
set<int> left;
long long ans = 0;
for(int i = 0; i < N; i ++){
left.insert(i);
arr[S[i]].push(i);
}
for(int i = 0; i < N / 2; i ++){
int s = *left.begin();
left.erase(s);
int b = arr[-S[s]].front();
left.erase(b);
arr[S[s]].pop();
arr[-S[s]].pop();
if(S[s] > 0){
ans += tree.sum(b+1) - tree.sum(s+1);
}
else{
ans += tree.sum(b+1) - tree.sum(s+1) - 1;
}
tree.rem(s+1);
tree.rem(b+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... |