Submission #278340

#TimeUsernameProblemLanguageResultExecution timeMemory
278340KastandaArranging Shoes (IOI19_shoes)C++14
100 / 100
111 ms20344 KiB
// M #include<bits/stdc++.h> //#include "shoes.h" using namespace std; const int N = 100055 * 2; int M[N], F[N]; vector < int > V[N][2]; inline void Add(int i, int val) { for (i += 5; i < N; i += i & - i) F[i] += val; } inline int Get(int i) { int rt = 0; for (i += 5; i; i -= i & - i) rt += F[i]; return rt; } long long count_swaps(vector < int > A) { int n = (int)A.size() / 2; for (int i = 0; i < n + n; i ++) V[abs(A[i])][A[i] > 0].push_back(i); int m = 0; for (int i = 1; i <= n; i ++) { assert(V[i][0].size() == V[i][1].size()); for (int j = 0; j < (int)V[i][0].size(); j ++) m ++, A[V[i][0][j]] = -m, A[V[i][1][j]] = m; } long long tot = 0; memset(M, -1, sizeof(M)); for (int i = 0; i < n + n; i ++) { if (M[abs(A[i])] == -1) Add(i, 1), M[abs(A[i])] = i; else { if (A[i] < 0) tot ++; Add(M[abs(A[i])], -1); tot += Get(M[abs(A[i])]) * 2LL; tot += Get(N - 10) - Get(M[abs(A[i])]); } } return tot; }
#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...