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>
using namespace std;
#define int long long
typedef vector<int> vi;
typedef vector<signed> vs;
typedef vector<vi> vii;
const int sizea = 200000;
const int sqrts = 316;
const int sqrtn = (sizea / sqrts + 1);
vi norms(sizea);
vi sqrtarr(sqrtn, 0);
void update(int b, int e, int v)
{
int idx = sizea;
for (int i = b; i < e; i++) {
norms[i] += v;
if (i % sqrts == sqrts - 1) {
idx = i + sqrts;
break;
}
}
for (int i = idx; i < e; i += sqrts) {
sqrtarr[i / sqrts] += v;
idx = i + 1;
}
for (int i = idx; i < e; i++) {
norms[i] += v;
}
}
int get(int i)
{
return norms[i] + sqrtarr[i / sqrts];
}
int gidx(int v, int n)
{
return v + (n / 2);
}
long long count_swaps(vs s_s)
{
vi s(s_s.size());
int n = s.size();
for (int i = 0; i < n; i++)
s[i] = s_s[i];
iota(norms.begin(), norms.end(), 0);
vi used(n, 0);
map<int, vector<int>> last;
set<int> idx;
for (int i = 0; i < n; i++)
idx.insert(s[i]);
for (int i = 0; i < n; i++)
last[s[i]].push_back(i);
for (int i : idx)
sort(last[i].begin(), last[i].end());
for (int i : idx)
reverse(last[i].begin(), last[i].end());
long long c = 0;
for (int i = 0; i < n; i++) {
if (used[i])
continue;
//cout << i << endl;
int r = -s[i];
int nxt = *prev(last[r].end());
last[s[i]].pop_back();
last[r].pop_back();
used[i] = 1;
used[nxt] = 1;
int d = get(nxt) - get(i);
d--;
if (s[i] > 0)
d++;
c += d;
update(i + 1, nxt, 1);
}
return c;
}
# | 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... |