제출 #408762

#제출 시각아이디문제언어결과실행 시간메모리
408762dxz05Arranging Shoes (IOI19_shoes)C++14
10 / 100
276 ms276416 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 4e5 + 6e3; int f[MAXN]; void add(int x, int y){ x++; while (x < MAXN){ f[x] += y; x += (-x & x); } } void add(int l, int r, int y){ if (l > r) return; add(l, y); add(r + 1, -y); } int get(int x){ x++; int sum = 0; while (x > 0){ sum += f[x]; x -= (-x & x); } return sum; } int get(int l, int r){ return get(r) - get(l - 1); } int code(int val){ return (val < 0 ? -val : val + 200000); } int original_position(int x){ int last = 0; while (true){ int y = get(x); if (y <= last) break; x += y - last; last = y; } return x; } queue<int> q[MAXN]; long long count_swaps(vector<int> a) { int n = a.size(); for (int i = 0; i < n; i++){ q[code(a[i])].push(i); } vector<bool> used(n, false); ll ans = 0; for (int i = 0; i < n; i++){ if (used[i]) continue; int x = code(a[i]), y = code(-a[i]); int pos1 = i, pos2 = q[y].front(); used[pos2] = true; pos1 = original_position(pos1), pos2 = original_position(pos2); //cout << pos1 << ' ' << pos2 << endl; ans += pos2 - pos1 - 1 + (a[i] > 0); add(pos1 + 1, pos2 - 1, 1); q[x].pop(); q[y].pop(); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...