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 ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
long long st123(vector<int> p, int s) {
vector<int> ori = p;
int n = (int)(p.size());
ll ans = 0;
for (int i = 0; i < n; i++) {
if (p[i] == -1 || p[i] == i) continue;
int cur = i;
vector<int> seq = {cur};
while (p[cur] != i) {
cur = p[cur];
seq.pb(cur);
}
// cerr << i << ": ";
// for (int x : seq) cerr << x << ' ';
// cerr << endl;
for (int i = 0; i < (int)(seq.size()); i++) {
ans += abs(seq[(i + 1) % (int)(seq.size())] - seq[i]);
p[seq[i]] = -1;
}
}
vector<int> bruh;
int maxi = 0;
for (int i = 0; i < n; i++) {
maxi = max(maxi, ori[i]);
if (maxi == i) bruh.pb(i);
}
// for (int x : bruh) cerr << x << ' ';
// cerr << endl;
while (bruh.size() >= 2) {
int x = bruh.size();
if (bruh[x - 1] - bruh[x - 2] == 1) {
bruh.pop_back();
} else break;
}
ans += ((int)(bruh.size()) - 1) * 2;
return ans;
}
long long minimum_walk(vector<int> p, int s) {
vector<int> ori = p;
int n = (int)(p.size());
ll ans = 0;
for (int i = 0; i < n; i++) {
if (p[i] == -1 || p[i] == i) continue;
int cur = i;
vector<int> seq = {cur};
while (p[cur] != i) {
cur = p[cur];
seq.pb(cur);
}
// cerr << i << ": ";
// for (int x : seq) cerr << x << ' ';
// cerr << endl;
for (int i = 0; i < (int)(seq.size()); i++) {
ans += abs(seq[(i + 1) % (int)(seq.size())] - seq[i]);
p[seq[i]] = -1;
}
}
vector<int> bruh = {-1};
int maxi = 0;
for (int i = 0; i < n; i++) {
maxi = max(maxi, ori[i]);
if (maxi == i) bruh.pb(i);
}
deque<pii> dq;
for (int i = 1; i < (int)(bruh.size()); i++) {
dq.push_back({bruh[i - 1] + 1, bruh[i]});
}
while (!dq.empty()) {
pii cur = dq.front();
if (cur.ff <= s && s <= cur.ss) break;
if (cur.ff < cur.ss) break;
dq.pop_front();
}
while (!dq.empty()) {
pii cur = dq.back();
if (cur.ff <= s && s <= cur.ss) break;
if (cur.ff < cur.ss) break;
dq.pop_back();
}
ans += ((int)(dq.size()) - 1) * 2;
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... |