# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
824188 | SoulKnight | Arranging Shoes (IOI19_shoes) | C++17 | 0 ms | 0 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;
#define ll long long
#define ln '\n'
const int N = 1e5 + 5;
const int LG = 20;
const int INF = 2e9 + 5;
const int MOD = 998244353;
vector<int> s, t;
int n, ft[N];
map<int, vector<int>> mp;
vector<pair<int, int>> vec;
void update(int p, int delta){
for (++p; p < N; p += (p & -p)) ft[p] += delta;
}
int sum(int p){
int res = 0;
for (++p; p > 0; p -= (p & -p)) res += ft[p];
return res;
}
void init(){
cin >> n;
s.resize(2*n);
for (int i = 0; i < 2*n; i++) cin >> s[i];
}
ll move(vector<int>& v){
int x = v.size();
ll ans = 0LL;
for (int i = 0; i < x; i++){
ans += v[i] - sum(v[i]);
update(v[i]+1, 1);
}
return ans;
}
ll solve(vector<int>& s){
n = s.size();
for (int i = 0; i < n; i++) mp[s[i]].push_back(i);
for (auto& p: mp){
if (p.first > 0) break;
sort(p.second.begin(), p.second.end());
sort(mp[-p.first].begin(), mp[-p.first].end());
for (int i = 0; i < p.second.size(); i++) vec.push_back({p.second[i], mp[-p.first][i]});
}
sort(vec.begin(), vec.end(), [&](const pair<int, int>& x, const pair<int, int>& y){
return min(x.first, x.second) < min(y.first, y.second);
});
// sort(vec.begin(), vec.end());
t.resize(n);
for (int i = 0; i < n; i+=2) {t[i] = vec[i/2].first; t[i+1] = vec[i/2].second;}
// for (auto& u: t) cout << u << ' ';
// cout << ln;
return move(t);
// t = {1,3,2,0};
// return move(t);
}
// int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);
// // freopen("mooyomooyo.in", "r", stdin);
// // freopen("mooyomooyo.out", "w", stdout);
// // ll T; cin >> T;
// // while (T--){
// // solve();
// // }
// init();
// cout << solve(s);
// return 0;
// }