Submission #780546

#TimeUsernameProblemLanguageResultExecution timeMemory
780546Minindu206Arranging Shoes (IOI19_shoes)C++14
0 / 100
63 ms134860 KiB
#include "shoes.h" #include <bits/stdc++.h> #define ll long long const int MAXN = 1e5 + 5; using namespace std; queue<int> pos[MAXN], neg[MAXN]; struct fenwick { int n; vector<int> fn; void init(int _n) { this->n = _n + 1; fn.resize(n, 0); } void update(int x, int y) { x++; while(x < n) { fn[x] += y; x += (x & (-x)); } } int sum(int ind) { ind++; int ans = 0; while(ind) { ans += fn[ind]; ind -= (ind & (-ind)); } return ans; } }; ll count_swaps(vector<int> s) { int n = s.size() / 2; for(int i=0;i<2 * n;i++) { if(s[i] > 0) pos[s[i]].push(i); else neg[-s[i]].push(i); } vector<int> vis(2 * n); fenwick tree; tree.init((2 * n)); ll ans = 0; for(int i=0;i<2*n;i++) { ll swps = 0; if(vis[i]) continue; if(s[i] > 0) { int nxt = neg[s[i]].front(); neg[s[i]].pop(); ans += (i - nxt) - (tree.sum(nxt + 1) - tree.sum(i + 1)); tree.update(nxt + 1, 1); vis[i] = 1; vis[nxt] = 1; } else { int nxt = pos[-s[i]].front(); pos[-s[i]].pop(); ans += (i - nxt - 1) - (tree.sum(nxt + 1) - tree.sum(i + 1)); tree.update(nxt + 1, 1); vis[i] = 1; vis[nxt] = 1; } } return ans; }

Compilation message (stderr)

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:54:12: warning: unused variable 'swps' [-Wunused-variable]
   54 |         ll swps = 0;
      |            ^~~~
#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...