Submission #377046

#TimeUsernameProblemLanguageResultExecution timeMemory
377046rocks03Arranging Shoes (IOI19_shoes)C++14
100 / 100
328 ms273516 KiB
//#pragma GCC target("avx2") //#pragma GCC optimization("O3") //#pragma GCC optimization("unroll-loops") #include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define ff first #define ss second #define pb push_back #define SZ(x) ((int)(x).size()) #define all(x) x.begin(), x.end() #define debug(x) cout << #x << ": " << x << " " #define rep(i, a, b) for(int i = (a); i < (b); i++) #define per(i, a, b) for(int i = (a); i >= (b); i--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); class FenwickTree{ public: int fensz; vector<int> fen; FenwickTree(int n){ fensz = n + 10; fen.resize(fensz); } void add(int i, int k){ ++i; for(; i < fensz; i += (i & -i)) fen[i] += k; } int get(int i){ ++i; int ans = 0; for(; i; i -= (i & -i)) ans += fen[i]; return ans; } }; long long count_swaps(vector<int> a){ int N = SZ(a); const int MAXS = 2e5+100; queue<int> ql[MAXS], qr[MAXS]; rep(i, 0, N){ if(a[i] < 0) ql[-a[i]].push(i); else qr[a[i]].push(i); } FenwickTree fen(N); vector<bool> used(N, false); ll ans = 0; rep(i, 0, N){ if(used[i]) continue; used[i] = true; if(a[i] < 0){ a[i] *= -1; ql[a[i]].pop(); assert(!qr[a[i]].empty()); int p = qr[a[i]].front(); qr[a[i]].pop(); ans += ((p + fen.get(p)) - (i + fen.get(i))) - 1; used[p] = true; fen.add(p + 1, -1); } else{ qr[a[i]].pop(); assert(!ql[a[i]].empty()); int p = ql[a[i]].front(); ql[a[i]].pop(); ans += ((p + fen.get(p)) - (i + fen.get(i))); used[p] = true; fen.add(p + 1, -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...