#include "shoes.h"
#include<bits/stdc++.h>
using namespace std;
#define debug(v) cerr << #v << ": " << v << " ";
#define debugln(v) cerr << #v << ": " << v << endl;
typedef long long ll;
const int MAXN = 1e5+5;
int n;
stack<int> freq[MAXN];
int pos2[2*MAXN];
int* pos = pos2+MAXN;
ll ans = 0;
bool cmp(int a, int b){
if(a == -b){
if(a<b){
ans += pos[a]-pos[b];
swap(pos[a], pos[b]);
return true;
}
// cerr << "n troca" << endl;
return false;
}
if( abs(a) < abs(b)){
ans += pos[a]-pos[b];
swap(pos[a], pos[b]);
return true;
}
// cerr << "n troca" << endl;
return false;
}
long long count_swaps(vector<int> s){
vector<int> filt(s.size());
ll cnt = 0;
for(int i = 0; i < (int)s.size(); i++){
if(s[i] < 0){
cnt++;
freq[-s[i]].push(i);
filt[i] = -cnt;
}
}
for(int i = s.size()-1; i >= 0; i--){
if(s[i] > 0){
filt[i] = -filt[freq[s[i]].top()];
freq[s[i]].pop();
}
}
vector<int> f;
for(int i = 0; i < (int)s.size(); i++){
f.push_back(filt[i]);
pos[filt[i]] = i;
}
sort(f.begin(), f.end(), cmp);
return ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |