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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
namespace debug {
const int DEBUG = false;
template<class T1, class T2>
void pr(const pair<T1, T2> &x);
template<class T, size_t SZ>
void pr(const array<T, SZ> &x);
template<class T>
void pr(const vector<T> &x);
template<class T>
void pr(const set<T> &x);
template<class T1, class T2>
void pr(const map<T1, T2> &x);
template<class T>
void pr(const T &x) { if (DEBUG) cout << x; }
template<class T, class... Ts>
void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }
template<class T1, class T2>
void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }
template<class T>
void prIn(const T &x) {
pr("{");
bool fst = 1;
for (auto &a : x) {
pr(fst ? "" : ", ", a), fst = 0;
}
pr("}");
}
template<class T, size_t SZ>
void pr(const array<T, SZ> &x) { prIn(x); }
template<class T>
void pr(const vector<T> &x) { prIn(x); }
template<class T>
void pr(const set<T> &x) { prIn(x); }
template<class T1, class T2>
void pr(const map<T1, T2> &x) { prIn(x); }
void ps() { pr("\n"), cout << flush; }
template<class Arg, class... Args>
void ps(const Arg &first, const Args &... rest) {
pr(first, " ");
ps(rest...);
}
}
using namespace debug;
int N;
vi nxt;
ll minimum_walk(vi p, int s) {
N = p.size();
// prune
int l = 0, r = N - 1;
while (l < s && p[l] == l) {
l++;
}
while (r > s && p[r] == r) {
r--;
}
for (int i = l; i <= r; i++) {
nxt.pb(p[i] - l);
}
N = nxt.size();
//ps("fin prune");
ll sum = 0;
for (int i = 0; i < N; i++) {
sum += abs(nxt[i] - i);
}
vector<vi> groups;
vi id(N, -1);
for (int i = 0; i < N; i++) {
if (id[i] == -1) {
vi newG;
int cur = i;
while (id[cur] == -1) {
id[cur] = groups.size();
newG.pb(cur);
cur = nxt[cur];
}
groups.pb(newG);
}
}
//ps("div groups");
vi lM, rM;
for (int i = 0; i < groups.size(); i++) {
int cLM = N, cRM = 0;
for (int x : groups[i]) {
cLM = min(cLM, x);
cRM = max(cRM, x);
}
lM.push_back(cLM);
rM.push_back(cRM);
}
//ps("prep");
sort(lM.begin(), lM.end());
int visRM = 0;
int nxtLM = 0;
while (nxtLM < groups.size()) {
int cX = lM[nxtLM];
if (cX > visRM) {
assert(cX == visRM + 1);
sum += 2;
}
visRM = max(rM[id[cX]], visRM);
nxtLM++;
}
return sum;
}
Compilation message (stderr)
books.cpp: In function 'll minimum_walk(vi, int)':
books.cpp:126:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < groups.size(); i++) {
~~^~~~~~~~~~~~~~~
books.cpp:142:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (nxtLM < groups.size()) {
~~~~~~^~~~~~~~~~~~~~~
# | 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... |