제출 #144865

#제출 시각아이디문제언어결과실행 시간메모리
144865eriksuenderhaufArranging Shoes (IOI19_shoes)C++14
100 / 100
431 ms276600 KiB
//#pragma GCC optimize("O3") #include <bits/stdc++.h> #include "shoes.h" #define pii pair<int, int> #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; typedef long long ll; const int MAXN = 2e5 + 5; deque<int> L[MAXN], R[MAXN]; int seg[MAXN*4], vis[MAXN]; void upd(int l, int r, int k, int a, int b) { if (a <= l && r <= b) { seg[k]++; return; } int m = (l+r) / 2; if (a <= m) upd(l,m,k*2,a,b); if (m < b) upd(m+1,r,k*2+1,a,b); } int qry(int l, int r, int k, int a) { if (l == r) return a+seg[k]; int m = (l+r) / 2; return seg[k] + (a <= m ? qry(l,m,k*2,a) : qry(m+1,r,k*2+1,a)); } ll count_swaps(vi s) { int n = s.size() / 2; deque<pii> act; for (int i = 0; i < 2*n; i++) { act.pb(mp(i,s[i])); if (s[i] < 0) L[-s[i]].pb(i); else R[s[i]].pb(i); } ll ret = 0; while (act.size()) { pii a1 = act.front(); act.pop_front(); if (vis[a1.fi]) continue; if (a1.se < 0) L[-a1.se].pop_front(); else R[a1.se].pop_front(); if (a1.se < 0) { int nx = R[-a1.se].front(); R[-a1.se].pop_front(); vis[nx] = 1; ret += (ll)abs(qry(0,2*n-1,1,a1.fi)+1-qry(0,2*n-1,1,nx)); upd(0,2*n-1,1,a1.fi,nx); } else { int nx = L[a1.se].front(); L[a1.se].pop_front(); vis[nx] = 1; ret += (ll)abs(qry(0,2*n-1,1,a1.fi)-qry(0,2*n-1,1,nx)); upd(0,2*n-1,1,a1.fi,nx); } } return ret; } /*int main() { int n; scanf("%d", &n); vi s(2*n); for (int i = 0; i < 2*n; i++) scanf("%d", &s[i]); printf("%I64d\n", count_swaps(s)); return 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...