Submission #776984

#TimeUsernameProblemLanguageResultExecution timeMemory
776984Trisanu_DasArranging Shoes (IOI19_shoes)C++17
50 / 100
186 ms277956 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long ll n, BIT[100005]; queue<ll> l_unpair[100005], r_unpair[100005]; void upd(int idx, ll val){ for(int i = idx; i < n + 1; i += (i & (-i))) BIT[i] += val; } ll qry(int idx){ ll ans = 0; for(int i = idx; i > 0; i -= (i & (-i))) ans += BIT[i]; return ans; } ll count_swaps(vector<int> s){ n = s.size(); ll swaps = 0; for(int i = 1; i < n + 1; i++){ ll shoe_sz = s[i - 1]; if(shoe_sz < 0){ shoe_sz *= -1; // extracting shoe size; if(!r_unpair[shoe_sz].empty()){ //has a shoe unpaired of this(shoe_sz) size. ll shoe_need = r_unpair[shoe_sz].front(); r_unpair[shoe_sz].pop(); swaps += qry(i) - qry(shoe_need - 1); upd(shoe_need, 1); }else{ // no shoe. l_unpair[shoe_sz].push(i); upd(i, 1); } }else{ if(!l_unpair[shoe_sz].empty()){ //has a shoe unpaired of this(shoe_sz) size. ll shoe_need = l_unpair[shoe_sz].front(); l_unpair[shoe_sz].pop(); swaps += qry(i) - qry(shoe_need); upd(shoe_need, 1); }else{ // no shoe. r_unpair[shoe_sz].push(i); upd(i, 1); } } } return swaps; }
#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...