# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1152123 | MPG | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
//#pragma GCC optomize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse4.1,sse4.2")
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define max_heap priority_queue<pair <ll, pair <ll, ll>>>
// #define min_heap priority_queue<pair <ll, pair <ll, ll>>, vector<pair <ll, pair <ll, ll>>>, greater<pair <ll, pair <ll, ll>>>>
// //#define min_heap priority_queue<ll, vector<ll>, greater<ll>>
// #define sariE cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);
// #define filE freopen("in.txt", "r", stdin); freopen("out1.txt", "w", stdout);
// #define endl '\n'
// #define md(a) (a % mod + mod) % mod
// #define pb push_back
// //cout << vectorprecision(5) << fixed << f;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const maxn = 2e5 + 123;
// ll const inf = 2e18;
// ll const loG = 23;
// ll const mod = 1e9 + 7;
// //ll const mod = 998244353;
// ll const sq = 350;
// ll power(ll a, ll b, ll mod){if(b==0)return 1;if(b==1)return a;ll x = power(a, b / 2, mod);return (((x * x) % mod) * (b % 2 ? a : 1)) % mod;}
queue <ll> chap[maxn], rast[maxn];
bool mark[maxn];
struct Fenw {
ll n;
vector<ll> t;
Fenw(ll n) : n(n), t(n + 1) {}
void update(ll i, ll x){
i++;
for(; i <= n; i += i & -i) t[i] += x;
}
int get(int i) {
int res = 0;
for(; i >= 1; i -= i & -i) res += t[i];
return res;
}
int get(int l, int r) {
return get(r + 1) - get(l);
}
};
ll count_swaps(vector <ll> s){
ll ans = 0;
Fenw bit(maxn);
for (int i = 0; i < s.size(); i++){
if (s[i] > 0){
rast[s[i]].push(i);
}
else
chap[-s[i]].push(i);
}
for (int i = 0; i < s.size(); i++){
if (mark[i])
continue;
mark[i] = 1;
ll j;
if (s[i] < 0){
chap[abs(s[i])].pop();
j = rast[abs(s[i])].front();
rast[abs(s[i])].pop();
}
else{
rast[s[i]].pop();
j = chap[s[i]].front();
chap[s[i]].pop();
}
mark[j] = 1;
ll chand = bit.get(i, j);
//cout << i << ' ' << j << ' ' << chand << endl;
if (s[i] < 0)
ans += j - i - 1 - chand;
else
ans += j - i - chand;
bit.update(i, 1);
bit.update(j, 1);
}
return ans;
}
// void Solve(){
// }
// int main(){
// sariE;// filE;
// int test = 1;
// //cin >> test;
// while (test--) Solve();
// return 0;
// }