Submission #875084

#TimeUsernameProblemLanguageResultExecution timeMemory
875084StefanL2005Arranging Shoes (IOI19_shoes)C++14
45 / 100
1095 ms142536 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long void update(int node, int left, int right, int point, vector<int> &tree) { if (left == right) tree[node] = 1; else { int m = (left + right) / 2; if (point <= m) update(2*node, left, m, point, tree); else update(2*node + 1, m + 1, right, point , tree); tree[node] = tree[2*node] + tree[2*node + 1]; } } int querry(int node, int l, int r, int ql, int qr, vector<int> &tree) { if (ql <= l && r <= qr) return tree[node]; int m = (l + r) / 2; if (qr <= m) return querry(2 * node, l, m, ql, qr, tree); if (ql > m) return querry(2 * node + 1, m + 1, r, ql, qr, tree); return querry(2 * node, l, m, ql, qr, tree) + querry(2 * node + 1, m + 1, r, ql, qr, tree); } int nr_ofTaken(int c1, int c2, vector<int> &Taken) { if (c1 == c2) return Taken[c1]; return Taken[c1] + nr_ofTaken(c1 + 1, c2, Taken); } ll count_swaps (vector<int> S) { int n = S.size() / 2; ll ans = 0; vector<int> Taken(2 * n, 0); vector<int> Tree(8 * n, 0); vector<deque<int>> Poz(2 * n + 2); for (int i = 0; i < 2 * n; i++) { if (S[i] < 0) Poz[abs(S[i])].push_back(i); else Poz[S[i] + n].push_back(i); } for (int i = 0; i < 2 * n - 1; i++) { if (Taken[i] == 1) continue; if (S[i] < 0 && 0 - S[i + 1] == S[i]) { Taken[i] = 1; Taken[i + 1] = 1; Poz[abs(S[i])].pop_front(); Poz[abs(S[i]) + n].pop_front(); continue; } Taken[i] = 1; // TREBUIE SA SCADEM SI NR DE TAKEN[J] = 1 UNDE J = i, need int need; if (S[i] < 0) { Poz[abs(S[i])].pop_front(); need = Poz[abs(S[i]) + n].front(); Poz[abs(S[i]) + n].pop_front(); ans += need - i - 1 - querry(1, 0, 2*n - 1, i + 1, need, Tree); Taken[need] = 1; update(1, 0, 2*n - 1, need, Tree); } else { Poz[abs(S[i]) + n].pop_front(); need = Poz[abs(S[i])].front(); Poz[abs(S[i])].pop_front(); ans += need - i - querry(1, 0, 2*n - 1, i + 1, need, Tree); Taken[need] = 1; update(1, 0, 2*n - 1, need, Tree); } } 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...