Submission #836274

#TimeUsernameProblemLanguageResultExecution timeMemory
836274AntekbArranging Shoes (IOI19_shoes)C++17
100 / 100
65 ms15180 KiB
#include<bits/stdc++.h> #include "shoes.h" #define st first #define nd second #define pb push_back #define eb emplace_back #define pp pop_back #define mp make_pair #define all(x) (x).begin(), (x).end() using namespace std; using pii = pair<int, int>; using vi = vector<int>; using vii = vector<pii>; using ll = long long; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); void debug(){cerr<<"\n";} template<typename H, typename... T> void debug(H h, T... t){ cerr<<h; if(sizeof...(t))cerr<<", "; debug(t...); } #define deb(x...) cerr<<#x<<" = ";debug(x); const int N=2e5+5; int tab[N+N]; void add(int v, int c){ for(v+=N; v; v>>=1){ tab[v]+=c; } } int sum(int l, int r){ int ans=0; for(l+=N, r+=N; l<r; l>>=1, r>>=1){ if(l&1)ans+=tab[l++]; if(r&1)ans+=tab[--r]; } return ans; } ll count_swaps(std::vector<int> s) { int n=s.size()/2; vector<vi> L(n), R(n); for(int i=0; i<n+n; i++){ if(s[i]>0)L[s[i]-1].pb(i); else R[-1-s[i]].pb(i); add(i, 1); } for(int i=0; i<n; i++){ reverse(all(R[i])); reverse(all(L[i])); } ll ans=0; for(int i=0; i<n+n; i++){ if(tab[N+i]){ int t=abs(s[i])-1; if(s[i]>0)ans++; int k=max(L[t].back(), R[t].back()); add(k, -1); ans+=sum(i+1,k); L[t].pp(); R[t].pp(); } } 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...