# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
922086 | Nurislam | Arranging Shoes (IOI19_shoes) | C++14 | 359 ms | 32420 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>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
//#define int long long
long long count_swaps(vector<int> p) {
int n = (int)p.size();
vector <int> bit(n+1);
auto upd = [&](int i, int val){
for (; i <= n; i += i&-i ) bit[i] += val;
};
auto get = [&](int i){
int cnt = 0;
assert(i <= n);
for (; i > 0; i -= i&-i ) cnt += bit[i];
return cnt;
};
for ( int i = 1; i <= n; i++ ) upd(i, 1);
long long cnt = 0;
map <int,multiset<int>> L, R;
for ( int i = 1; i <= n; i++ ){
if ( p[i-1] < 0 ) L[p[i-1]].insert(i);
else R[p[i-1]].insert(i);
}
for ( int i = 1; i <= n; i++ ){
if ( !p[i-1] ) continue;
if ( p[i-1] < 0 ){
L[p[i-1]].erase(L[p[i-1]].begin());
assert(!R[-p[i-1]].empty());
auto it = *R[-p[i-1]].begin();
R[-p[i-1]].erase(R[-p[i-1]].begin());
cnt += get(it)-get(i)-1;
p[it-1] = 0;
upd(i, 1), upd(it+1, -1);
}
else{
R[p[i-1]].erase(R[p[i-1]].begin());
assert(!L[-p[i-1]].empty());
auto it = *L[-p[i-1]].begin();
L[-p[i-1]].erase(L[-p[i-1]].begin());
cnt += get(it)-get(i);
p[it-1] = 0;
upd(i, 1), upd(it+1, -1);
}
}
return cnt;
}
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... |