Submission #399235

#TimeUsernameProblemLanguageResultExecution timeMemory
399235_Avocado_Arranging Shoes (IOI19_shoes)C++14
45 / 100
375 ms93976 KiB
#include <bits/stdc++.h> #include <iostream> using namespace std; #define int int64_t vector<int>seg; void update(int k, int v, int pos, int l, int r){ if(l == r){ seg[pos] = v; return; } else if(k <= (l+r)/2) update(k, v, pos*2, l, (r+l)/2); else update(k, v, pos*2+1, (l+r)/2+1, r); seg[pos] = seg[pos*2] + seg[pos*2+1]; } int query(int ql, int qr, int pos, int l, int r){ if(l >= ql && r <= qr) return seg[pos]; else if(l > qr || r < ql) return 0; else return query(ql, qr, pos*2, l, (l+r)/2) + query(ql, qr, pos*2+1, (l+r)/2+1, r); } long long count_swaps(std::vector<int32_t>v){ int n = v.size(); map<int, queue<int>>mp; vector<int>shoes(n); int cur = 1; for(int i = 0; i<n; ++i){ if(v[i] < 0){ mp[v[i]].push(cur); shoes[i] = cur; cur += 2; } } for(int i = 0; i<n; ++i){ if(v[i] > 0){ shoes[i] = mp[-v[i]].front()+1; mp[-v[i]].pop(); } } //for(auto u: shoes) cout<<u<<" "; vector<vector<int>>fred(n); seg.assign(n*4, 0); for(int i = 0; i<n; ++i){ fred[i] = {shoes[i], i}; } sort(fred.begin(), fred.end()); reverse(fred.begin(), fred.end()); int ans = 0; for(int i = 0; i<n; ++i){ ans += query(0, fred[i][1], 1, 0, n-1); update(fred[i][1], 1, 1, 0, n-1); } 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...