# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
58404 | reality | Ancient Books (IOI17_books) | C++17 | 3 ms | 484 KiB |
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 fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
#include "books.h"
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
ll ans = 0;
int mx = 0;
vi was(n,0);
vector < pii > e;
for (int i = 0;i < n;++i)
if (!was[i]) {
int cnt = i;
int mx = 0;
do {
was[cnt] = 1;
smax(mx,cnt);
cnt = p[cnt];
} while (cnt != i);
e.pb(mp(i,mx));
}
for (int i = 0;i < n;++i)
ans += abs(i - p[i]);
int k = e.size();
ans += k * (k - 1);
int j = 0;
for (int i = 0;i < k;++i) {
while (j + 1 < k && e[j + 1].fi <= e[i].se)
++j;
ans -= 2 * (j - i);
}
return ans;
}
Compilation message (stderr)
# | 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... |