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 <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
int nxt[100005][2];
bool past[100005] ;
int find_next(int k, int positive){
if(!past[k])return k;
if(!past[nxt[k][positive]])return nxt[k][positive];
return nxt[k][positive] = find_next(nxt[k][positive], positive); // siempre tiene que existir.
}
long long count_swaps(std::vector<int> s) {
ll n = s.size();
int siguiente[n][2]; // 0 negative 1 positive
for(int i = 1 ; i <= n ; i ++){
siguiente[i][0] = -1;
siguiente[i][1] = -1;
nxt[i][0] = -1;
nxt[i][1] = -1;
}
for(int i = n-1 ; i >= 0 ; i--){
nxt[i][0] = siguiente[abs(s[i])][0];
nxt[i][1] = siguiente[abs(s[i])][1];
if(s[i] > 0)siguiente[abs(s[i])][1] = i;
else siguiente[abs(s[i])][0] = i;
}
memset(past, false, sizeof past);
ll ans = 0;
for(int i = 0 ; i < n ; i ++){
if(past[i] )continue;
int busca = 1;
if(s[i] > 0)busca = 0;
//cout << "buscando para " << i ;
int k = find_next(nxt[i][busca], busca);
//cout << " desde " << k << endl;
for(int j = k-1 ; j > i ; j --){
if(!past[j])ans ++;
}
if(busca == 0)ans ++;
past[k] = true;
past[i] = true;
}
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... |