#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 minimum_walk(vector<int> p, int s) {
vector<int> ori = p;
int n = (int)(p.size());
ll ans = 0;
vector<pii> range(n);
for (int i = 0; i < n; i++) {
if (p[i] == -1) continue;
int cur = i;
vector<int> seq = {cur};
while (p[cur] != i) {
cur = p[cur];
seq.pb(cur);
}
pii get = {i, i};
for (int x : seq) {
get.ff = min(get.ff, x);
get.ss = max(get.ss, x);
}
for (int i = 0; i < (int)(seq.size()); i++) {
ans += abs(seq[(i + 1) % (int)(seq.size())] - seq[i]);
range[seq[i]] = get;
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;
if (s == 0) return ans;
pii tar;
while (!dq.empty()) {
pii cur = dq.front(); dq.pop_front();
if (cur.ff <= s && s <= cur.ss) {
tar = cur; break;
}
}
assert(n <= 1000);
vector<vector<int>> dp(n);
for (int i = 0; i < n; i++) dp[i].resize(n, 1e9);
dp[range[s].ff][range[s].ss] = 0;
queue<pii> q;
q.push(range[s]);
while (!q.empty()) {
pii cur = q.front(); q.pop();
if (cur.ff > 0) {
pii nrange = {range[cur.ff - 1].ff, max(cur.ss, range[cur.ff - 1].ss)};
if (dp[nrange.ff][nrange.ss] > dp[cur.ff][cur.ss] + 1) {
dp[nrange.ff][nrange.ss] = dp[cur.ff][cur.ss] + 1;
q.push(nrange);
}
}
if (cur.ss < n - 1) {
pii nrange = {min(cur.ff, range[cur.ss + 1].ff), range[cur.ss + 1].ss};
if (dp[nrange.ff][nrange.ss] > dp[cur.ff][cur.ss] + 1) {
dp[nrange.ff][nrange.ss] = dp[cur.ff][cur.ss] + 1;
q.push(nrange);
}
}
}
ans += dp[tar.ff][tar.ss] * 2;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
508 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
440 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
508 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
440 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
125 ms |
29380 KB |
Output is correct |
31 |
Correct |
112 ms |
27356 KB |
Output is correct |
32 |
Correct |
118 ms |
37460 KB |
Output is correct |
33 |
Correct |
99 ms |
33132 KB |
Output is correct |
34 |
Correct |
121 ms |
33532 KB |
Output is correct |
35 |
Correct |
122 ms |
30816 KB |
Output is correct |
36 |
Correct |
96 ms |
26820 KB |
Output is correct |
37 |
Correct |
99 ms |
25276 KB |
Output is correct |
38 |
Correct |
88 ms |
25424 KB |
Output is correct |
39 |
Correct |
79 ms |
25436 KB |
Output is correct |
40 |
Correct |
90 ms |
25608 KB |
Output is correct |
41 |
Correct |
100 ms |
28344 KB |
Output is correct |
42 |
Correct |
95 ms |
27000 KB |
Output is correct |
43 |
Correct |
97 ms |
25168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4188 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3314' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
508 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
440 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
125 ms |
29380 KB |
Output is correct |
31 |
Correct |
112 ms |
27356 KB |
Output is correct |
32 |
Correct |
118 ms |
37460 KB |
Output is correct |
33 |
Correct |
99 ms |
33132 KB |
Output is correct |
34 |
Correct |
121 ms |
33532 KB |
Output is correct |
35 |
Correct |
122 ms |
30816 KB |
Output is correct |
36 |
Correct |
96 ms |
26820 KB |
Output is correct |
37 |
Correct |
99 ms |
25276 KB |
Output is correct |
38 |
Correct |
88 ms |
25424 KB |
Output is correct |
39 |
Correct |
79 ms |
25436 KB |
Output is correct |
40 |
Correct |
90 ms |
25608 KB |
Output is correct |
41 |
Correct |
100 ms |
28344 KB |
Output is correct |
42 |
Correct |
95 ms |
27000 KB |
Output is correct |
43 |
Correct |
97 ms |
25168 KB |
Output is correct |
44 |
Incorrect |
3 ms |
4188 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3314' |
45 |
Halted |
0 ms |
0 KB |
- |