Submission #350660

#TimeUsernameProblemLanguageResultExecution timeMemory
350660pheniusArranging Shoes (IOI19_shoes)C++14
100 / 100
48 ms5136 KiB
#include "shoes.h" using namespace std; #define MAXN 100000 int a[2 * MAXN]; int shoes[2 * MAXN]; int index[2 * MAXN + 1]; int cnt[MAXN], cntl[MAXN]; int N; void clear_array(int *ar, int n) { for (int i = 0; i < n; i++) ar[i] = 0; } void copy_arr(int *a1, int *a2, int n) { for (int i = 0; i < n; i++) a1[i] = a2[i]; } void copy_arr(int *a1, vector <int> a2, int n) { for (int i = 0; i < n; i++) a1[i] = a2[i]; } class BIT { int count(int right) { int cnt = 0; for (; right >= 0; right = (right & (right + 1)) - 1) { cnt += a[right]; } return cnt; } public: BIT() { clear_array(a, 2 * N); } int count(int left, int right) { return count(right) - count(left - 1); } void insert(int index) { for (; index < 2 * N; index = index | (index + 1)) { a[index]++; } } }; void create_index() { clear_array(index, 2 * N + 1); for (int i = 0; i < 2 * N; i++) { index[shoes[i] + N] = i; } } void convert() { clear_array(cnt, N); for (int i = 0; i < 2 * N; i++) if (shoes[i] > 0) cnt[shoes[i] - 1]++; for (int i = 1; i < N; i++) cnt[i] += cnt[i - 1]; copy_arr(cntl, cnt, N); for (int i = 0; i < 2 * N; i++) if (shoes[i] > 0) shoes[i] = (--cntl[shoes[i] - 1]) + 1; else shoes[i] = -((--cnt[-shoes[i] - 1]) + 1); } long long count_swaps(vector<int> S) { N = S.size() / 2; copy_arr(shoes, S, 2 * N); convert(); create_index(); BIT tree = BIT(); long long ans = 0; for (int i = 0; i < 2 * N; i++) { if (shoes[i] != 0) { int pos = index[-shoes[i] + N]; ans += pos - i - tree.count(i, pos) - (shoes[i] < 0 ? 1 : 0); shoes[pos] = 0; tree.insert(pos); } } 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...