Submission #299088

#TimeUsernameProblemLanguageResultExecution timeMemory
299088ASDF123Arranging Shoes (IOI19_shoes)C++17
10 / 100
2 ms384 KiB
#include <bits/stdc++.h> #include "shoes.h" //#include "grader.cpp" using namespace std; const int N = (int)1e5 + 5; int tree[N * 4]; int a[N]; bool used[N]; map <int, int> position; void upd(int pos, int val, int v = 0, int tl = 0, int tr = N) { if (tl == tr) { tree[v] += val; return; } int mid = (tl + tr) >> 1; if (pos <= mid) { upd(pos, val, v + v + 1, tl, mid); } else { upd(pos, val, v + v + 2, mid + 1, tr); } tree[v] = tree[v + v + 1] + tree[v + v + 2]; } int get(int l, int r, int v = 0, int tl = 0, int tr = N) { if (l > r) { return 0; } if (l <= tl && tr <= r) { return tree[v]; } if (l > tr || tl > r) { return 0; } int mid = (tl + tr) >> 1; return get(l, r, v + v + 1, tl, mid) + get(l, r, v + v + 2, mid + 1, tr); } long long count_swaps(vector<int> a) { int n = (int)a.size() / 2; for (int i = 0; i < n * 2; i++) { cin >> a[i]; position[a[i]] = i; } long long ans = 0; for (int i = 0; i < n * 2; i++) { if (used[abs(a[i])]) { continue; } used[abs(a[i])] = 1; int r = position[-a[i]]; int add = (r + get(r + 1, n * 2 - 1)) - (i + get(i, n * 2 - 1) + 1); if (a[i] > 0) { add++; } ans += add; upd(r, 1); } 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...