# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
298266 | Rayaabualjamal | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shoes.h"
#include <cassert>
#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#define rep(i,a,b) for(int i = a; i<b; i++)
#define per(i,a,b) for(int i = a; i>=b; i--)
#define pb push_back
#define se second
using namespace std;
long long count_swaps(vector<int> s) {
int n = s.size();
long long ans = 0;
map <int, vector <int> > m;
rep(i,0,n){
m[s[i]].pb(i);
}
for(auto& vec:m){
reverse(vec.se.begin(), vec.se.end());
}
vector <int> vis(n+n, 0);
rep(i,0,n){
if(vis[i])continue;
//cout << i << " ";
if(s[i]<0){
int pos = m[-s[i]].back();
//cout << pos << " 1" << endl;
per(j,pos-1, i+1){
if(vis[j]==0)ans++;
}
vis[pos] = 1;
}else{
int pos = m[-s[i]].back();
//cout << pos << " 2" << endl;
per(j,pos-1, i){
if(vis[j]==0)ans++;
}
vis[pos] = 1;
}
m[-s[i]].pop_back();
m[s[i]].pop_back();
vis[i]=1;
}
//cout << "ans: " << ans << endl;
return ans;
}