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 "books.h"
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define MAX 1000000
lli n,ini,fin,res,last,activos,a,act;
lli vis[MAX+2];
vector<pair<lli,lli> > eventos;
long long minimum_walk(std::vector<int> p, int s) {
n = p.size();
res = 0;
rep(i,0,n-1) {
if (vis[i]) continue;
if (p[i] == i) continue;
vis[i] = 1;
ini = i;
fin = i;
act = p[i];
res += abs(i - act);
while (act != i) {
vis[act] = 1;
ini = min(ini,act);
fin = max(fin,act);
res += abs(p[act] - act);
act = p[act];
}
eventos.push_back({ini,0});
eventos.push_back({fin,1});
}
sort(eventos.begin(), eventos.end());
last = 0;
activos = 0;
for (auto e : eventos) {
if (activos == 0) {
a = e.first - last;
a *= 2;
res += a;
}
last = e.first;
if (e.second) activos--;
else activos++;
}
return res;
}
# | 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... |