#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;
bool vis[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;
}
int v1 = (v.size() > 1 ? v[1] : 0);
ans += v.back() - v1;
cout << ans << endl;
int cur = p[0];
int add = v.back();
while (cur != 0) {
//cout << v1 << ' ' << cur << ' ' << v.back() << endl;
add = min(add, abs(v1 - cur) + abs(v.back() - p[cur]) - abs(cur - p[cur]));
cur = p[cur];
}
ans += add;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |