제출 #250074

#제출 시각아이디문제언어결과실행 시간메모리
250074DavidDamianArranging Shoes (IOI19_shoes)C++14
100 / 100
425 ms149752 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; ///Binary Indexed Tree ///Determine the minimum number of swaps to obtain a valid sequence ///Remap the elements and find the number of invertions ///The same problem than trying to sort an array with numbers from 1 to N ll bit[400005]; ll query(int i) { ll sum=0; while(i>0){ sum+=bit[i]; i-=i&(-i); } return sum; } ll getSum(int a,int b) { return query(b)-query(a-1); } void update(int n,int i,ll v) { while(i<=n){ bit[i]+=v; i+=i&(-i); } } map<int,queue<int> > pairing; int link[400005]; ll count_swaps(vector<int> s) { int n=s.size(); for(int i=0;i<n;i++){ //Pair each element with its closest opposite if(pairing[ -s[i] ].empty()){ pairing[ s[i] ].push(i); } else{ int id=pairing[ -s[i] ].front(); pairing[ -s[i] ].pop(); link[id]=i; link[i]=id; } } int c=0; for(int i=0;i<n;i++){ //Remap the data if(link[i]<i) continue; if(s[i]<0) s[i]=++c, s[ link[i] ]=++c; else s[ link[i] ]=++c, s[i]=++c; } ll total=0; for(int i=0;i<n;i++){ //Find inversions total+=getSum(s[i]+1,n+1); update(n+1,s[i],1); } return total; }
#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...