# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1279154 | lazylettuce | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
// _ _ _ _
//| | __ _ _____ _| | ___| |_| |_ _ _ ___ ___
//| |/ _` |_ / | | | |/ _ \ __| __| | | |/ __/ _ \lazylettuce
//| | (_| |/ /| |_| | | __/ |_| |_| |_| | (_| __/
//|_|\__,_/___|\__, |_|\___|\__|\__|\__,_|\___\___|
// |___/
#include<bits/stdc++.h>
using namespace std;
using ll =long long ;
int MOD1=998244353;
int MOD2=1e9+7;
struct Fenwick {
int n;
vector<ll> bit;
Fenwick(int n_ = 0) { init(n_); }
void init(int n_) { n = n_; bit.assign(n+1, 0); }
void update(int idx, ll delta = 1) {
if (idx <= 0) return;
for (; idx <= n; idx += idx & -idx) bit[idx] += delta;
}
ll query(int idx) {
if (idx <= 0) return 0;
ll res = 0;
for (; idx > 0; idx -= idx & -idx) res += bit[idx];
return res;
}
ll total() { return query(n); }
ll inv_count(const vector<int>& v) {
fill(bit.begin(), bit.end(), 0);
ll inv = 0;
for (int x : v) {
if (x < 1 || x > n) {
// skip bad indices (defensive)
continue;
}
inv += (total() - query(x));
update(x, 1);
}
return inv;
}
};
long long count_swaps(vector<int>&S){
vector<int>temp;
vector<int>first;
vector<int>second;
for(int i=0;i<S.size();i++){
first.push_back(S[i]+501);
if(S[i]<0){
temp.push_back(S[i]);
}
}
for(auto it:temp){
second.push_back(it+501);
second.push_back((-it)+501);
}
Fenwick f(1001);
int ans=f.inv_count(first);
int ans2=f.inv_count(second);
return abs(ans-ans2);
}
// int32_t main(){
// ios::sync_with_stdio(false);
// int ji;
// cin>>ji;
// vector<int>v1(ji);
// for(int i=0;i<ji;i++){
// cin>>v1[i];
// }
// cout<<count_swaps(v1)<<endl;
// }