Submission #418753

#TimeUsernameProblemLanguageResultExecution timeMemory
418753nafis_shifatArranging Shoes (IOI19_shoes)C++14
100 / 100
94 ms20292 KiB
#include "shoes.h" #include<bits/stdc++.h> #define ll long long const int mxn = 2e5+ 5; using namespace std; struct BIT { int bit[mxn] = {}; void update(int p,int v) { for(; p < mxn; p += p & -p) bit[p] += v; } int query(int p) { int res = 0; for(; p > 0; p -= p & -p) res += bit[p]; return res; } int query(int a,int b) { if(a > b) return 0; return query(b) - query(a - 1); } } bt; ll count_swaps(vector<int> s) { int n = s.size(); vector<int> left[n],right[n]; for(int i = n - 1; i >= 0; i--) { if(s[i] < 0) left[-s[i]].push_back(i); else right[s[i]].push_back(i); } int vis[n + 1] = {}; ll res = 0; for(int i = 0; i < n; i++) { if(vis[i]) { continue; } if(s[i] < 0) { int x = -s[i]; assert(i == left[x].back()); left[x].pop_back(); int y = right[x].back(); res += y - i - 1 - bt.query(i + 1,y - 1); vis[y] = 1; right[x].pop_back(); bt.update(y,1); } else { int x = s[i]; assert(i == right[x].back()); right[x].pop_back(); int y = left[x].back(); res += y - i - bt.query(i + 1,y - 1); vis[y] = 1; left[x].pop_back(); bt.update(y,1); } } return res; }
#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...