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 pb push_back
#define mod 1000000007
#define h1 7897897897897897
#define h2 7897466719774591
#define b1 98762051
#define b2 98765431
#define inf 1000000000
#define pi 3.1415926535897932384626
#define LMAX 9223372036854775807
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pii>
#define SET(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
bool first_cycle;
int p[1000005], n;
int d[1000005];
bool vis[1000005], l[1000005], r[1000005], used[1000005];
ll ans;
void dfs(int x) {
if (vis[x]) return;
vis[x] = true;
ans += abs(x - p[x]);
dfs(p[x]);
}
long long minimum_walk(vector<int> perm, int s) {
n = perm.size();
FOR(i, 0, n-1) p[i] = perm[i];
int cycle = 0;
vi v;
FOR(i, 0, n-1) {
if (!vis[i]) {
cycle++;
if (p[i] == i) {
vis[i] = true;
continue;
}
dfs(i);
v.pb(i);
}
}
if (cycle == 1) {
return ans;
}
if (v.size() == 0) return 0;
FOR(i, 0, n-1) {
d[min(i, p[i])]++;
d[max(i, p[i])]--;
}
FOR(i, 1, n-1) {
d[i] += d[i-1];
}
FORD(i, n-2, 0) {
if (r[i + 1] || d[i] > 0) {
r[i] = true;
//cout << i << ' ' << r[i] <<' ' <<d[i] << endl;
}
}
FOR(i, 0, n-1) {
if (r[i] && d[i] == 0) {
ans += 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... |