This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// Created by adavy on 2/11/2023.
//
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using ii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vii = vector<ii>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define f first
#define s second
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define len(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
vi numShoes;
int n;
void ad(int i, int x){
while(i<=n){
numShoes[i] += x;
i += i&(-i);
}
}
int sm(int i){
int s=0;
//cout << i << endl;
while(i>=1) {
s += numShoes[i];
i -= i & (-i);
}
return s;
}
ll count_swaps(vi S){
n = len(S);
vi shoes(n);
F0R(i, n) shoes[i] = S[i];
numShoes.resize(n+1);
fill(all(numShoes), 0);
FOR(i, 1, n+1) ad(i, 1);
V<priority_queue<int, vector<int>, greater<int>>> pos(n+1);
V<priority_queue<int, vector<int>, greater<int>>> neg(n+1);
F0R(sh, len(shoes)){
if(shoes[sh]>0) pos[shoes[sh]].push(sh);
else neg[-shoes[sh]].push(sh);
}
//cout << "Foo" << endl;
ll tot = 0;
F0R(sh, len(shoes)){
if (shoes[sh]==0) continue;
//cout << sh << " "<< shoes[sh] << endl;
if(shoes[sh]>0){
pos[shoes[sh]].pop();
int next_sh = neg[shoes[sh]].top(); neg[shoes[sh]].pop();
//cout << "Bar" << endl;
shoes[next_sh] = 0;
tot += sm(next_sh) - sm(sh);
ad(next_sh+1, -1); ad(sh+1, -1);
}
else{
neg[-shoes[sh]].pop();
int next_sh = pos[-shoes[sh]].top(); pos[-shoes[sh]].pop();
shoes[next_sh] = 0;
tot += sm(next_sh) - sm(sh+1);
ad(next_sh+1, -1); ad(sh+1, -1);
}
}
return tot;
}
/*int main(){
int k;
cin >> k;
vi shs(k);
F0R(i, k) cin >> shs[i];
cout << count_swaps(shs) << endl;
}*/
# | 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... |