Submission #572527

#TimeUsernameProblemLanguageResultExecution timeMemory
572527MadokaMagicaFanArranging Shoes (IOI19_shoes)C++14
50 / 100
64 ms23360 KiB
#include "bits/stdc++.h" using namespace std; using ll = long long; const ll inf = 1e9; const int md1 = 1e9+7; const int md2 = 998244353; #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define sz(v) ((int)v.size()) #define forn(i,n) for(int i = 0; i < n; ++i) #define forbe(i,b,e) for(int i = b; i < e; ++i) #define pb push_back #define pry puts("YES") #define prn puts("NO") #define endl '\n' #define fst first #define scn second const int N = 1e5; bitset<N+2> c; vector<int> pp[N+1]; vector<int> np[N+1]; int pnt[N+1]; template<typename T> struct BIT{ int n; vector<T> fen; BIT(int n){ this->n = n; fen.assign(n,0); } BIT(vector<T> a) : BIT(a.size()){ for(int i = 0; i < n; ++i) add(i,a[i]); } void add(int x, T v){ for(; x < n; x |= x+1) fen[x] += v; } T query(int x){ T res = 0; for(; x >= 0; x = (x&(x+1))-1) res += fen[x]; return res; } T query(int l, int r){ return query(r) - query(l-1); } }; ll count_swaps(vector<int> s) { ll ans = 0; for (int i = 0; i < sz(s); ++i) { if (s[i] > 0) pp[s[i]].pb(i); if (s[i] < 0) np[-s[i]].pb(i); } BIT<int> deltr(sz(s)+5); int pos; for (int i = 0; i < sz(s); ++i) { if (c[i]) continue; if (s[i] > 0) { pos = np[abs(s[i])][pnt[abs(s[i])]]; ans += pos - deltr.query(i,pos) - i; c[pos] = 1; deltr.add(pos,1); } else { pos = pp[abs(s[i])][pnt[abs(s[i])]]; ans += pos - deltr.query(i,pos) - 1 - i; c[pos] = 1; deltr.add(pos,1); } ++pnt[abs(s[i])]; } return ans; } #ifdef ONPC void solve() { int n; cin >> n; vector<int> s(n); for (int i = 0; i < n; ++i) cin >> s[i]; cout <<count_swaps(s) << endl; } int32_t main() { freopen("in", "r", stdin); int t = 1; /* cin >> t; */ while(t--) solve(); } #endif
#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...