#include "books.h"
#include <bits/stdc++.h>
#define endl '\n'
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define debug cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7, inf = 1e9 + 7;
const ll hs = 199;
const ldb eps = 1e-9, pi = acos(-1);
using namespace std;
int n;
vector<int> a, vis, to;
int cycle(int st) {
vis[st] = 1;
int rtn = st;
st = a[st];
while (!vis[st]) {
rtn = max(rtn, st);
vis[st] = 1;
st = a[st];
}
return rtn;
}
ll minimum_walk(vector<int> p, int s) {
n = p.size();
a = p;
ll sum = 0;
for (int i = 0; i < n; i++)
sum += abs(i - a[i]);
int l = 0, r = n - 1;
while (l < n && a[l] == l) l++;
if (l == n) return 0;
while (a[r] == r) r--;
vis.resize(n, 0);
to.resize(n, -1);
for (int i = l; i <= r; i++)
if (!vis[i])
to[i] = cycle(i);
vector<pair<int, int>> st;
for (int i = l; i <= r; i++)
if (to[i] != -1) {
if (!st.size()) st.push_back({ i, to[i] });
else {
if (st.back().second < i) st.push_back({ i, to[i] });
else st.back().second = max(st.back().second, to[i]);
}
}
ll rtn = sum + 2LL * ((int)st.size() - 1);
if (s < l) rtn += 2 * (l - s);
if (r < s) rtn += 2 * (s - r);
return rtn;
}
/*
int main() {
int len, start;
vector<int> seq;
cin >> len >> start;
seq.resize(len);
for (auto &i : seq) cin >> i;
cout << minimum_walk(seq, start) << endl;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
4 ms |
524 KB |
Output is correct |
5 |
Correct |
3 ms |
524 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
544 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
668 KB |
Output is correct |
16 |
Correct |
3 ms |
668 KB |
Output is correct |
17 |
Correct |
2 ms |
668 KB |
Output is correct |
18 |
Correct |
2 ms |
668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
4 ms |
524 KB |
Output is correct |
5 |
Correct |
3 ms |
524 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
544 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
668 KB |
Output is correct |
16 |
Correct |
3 ms |
668 KB |
Output is correct |
17 |
Correct |
2 ms |
668 KB |
Output is correct |
18 |
Correct |
2 ms |
668 KB |
Output is correct |
19 |
Correct |
2 ms |
688 KB |
Output is correct |
20 |
Correct |
2 ms |
688 KB |
Output is correct |
21 |
Correct |
2 ms |
688 KB |
Output is correct |
22 |
Correct |
3 ms |
688 KB |
Output is correct |
23 |
Correct |
3 ms |
688 KB |
Output is correct |
24 |
Correct |
2 ms |
688 KB |
Output is correct |
25 |
Correct |
2 ms |
696 KB |
Output is correct |
26 |
Correct |
4 ms |
700 KB |
Output is correct |
27 |
Correct |
3 ms |
700 KB |
Output is correct |
28 |
Correct |
2 ms |
800 KB |
Output is correct |
29 |
Correct |
2 ms |
800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
4 ms |
524 KB |
Output is correct |
5 |
Correct |
3 ms |
524 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
544 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
668 KB |
Output is correct |
16 |
Correct |
3 ms |
668 KB |
Output is correct |
17 |
Correct |
2 ms |
668 KB |
Output is correct |
18 |
Correct |
2 ms |
668 KB |
Output is correct |
19 |
Correct |
2 ms |
688 KB |
Output is correct |
20 |
Correct |
2 ms |
688 KB |
Output is correct |
21 |
Correct |
2 ms |
688 KB |
Output is correct |
22 |
Correct |
3 ms |
688 KB |
Output is correct |
23 |
Correct |
3 ms |
688 KB |
Output is correct |
24 |
Correct |
2 ms |
688 KB |
Output is correct |
25 |
Correct |
2 ms |
696 KB |
Output is correct |
26 |
Correct |
4 ms |
700 KB |
Output is correct |
27 |
Correct |
3 ms |
700 KB |
Output is correct |
28 |
Correct |
2 ms |
800 KB |
Output is correct |
29 |
Correct |
2 ms |
800 KB |
Output is correct |
30 |
Correct |
301 ms |
26956 KB |
Output is correct |
31 |
Correct |
376 ms |
33876 KB |
Output is correct |
32 |
Correct |
174 ms |
33876 KB |
Output is correct |
33 |
Correct |
194 ms |
55640 KB |
Output is correct |
34 |
Correct |
214 ms |
62340 KB |
Output is correct |
35 |
Correct |
209 ms |
65068 KB |
Output is correct |
36 |
Correct |
194 ms |
69656 KB |
Output is correct |
37 |
Correct |
200 ms |
74364 KB |
Output is correct |
38 |
Correct |
206 ms |
81176 KB |
Output is correct |
39 |
Correct |
192 ms |
87852 KB |
Output is correct |
40 |
Correct |
246 ms |
94648 KB |
Output is correct |
41 |
Correct |
213 ms |
101244 KB |
Output is correct |
42 |
Correct |
239 ms |
108060 KB |
Output is correct |
43 |
Correct |
167 ms |
114732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
114732 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
4 ms |
524 KB |
Output is correct |
5 |
Correct |
3 ms |
524 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
544 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
668 KB |
Output is correct |
16 |
Correct |
3 ms |
668 KB |
Output is correct |
17 |
Correct |
2 ms |
668 KB |
Output is correct |
18 |
Correct |
2 ms |
668 KB |
Output is correct |
19 |
Correct |
2 ms |
688 KB |
Output is correct |
20 |
Correct |
2 ms |
688 KB |
Output is correct |
21 |
Correct |
2 ms |
688 KB |
Output is correct |
22 |
Correct |
3 ms |
688 KB |
Output is correct |
23 |
Correct |
3 ms |
688 KB |
Output is correct |
24 |
Correct |
2 ms |
688 KB |
Output is correct |
25 |
Correct |
2 ms |
696 KB |
Output is correct |
26 |
Correct |
4 ms |
700 KB |
Output is correct |
27 |
Correct |
3 ms |
700 KB |
Output is correct |
28 |
Correct |
2 ms |
800 KB |
Output is correct |
29 |
Correct |
2 ms |
800 KB |
Output is correct |
30 |
Correct |
301 ms |
26956 KB |
Output is correct |
31 |
Correct |
376 ms |
33876 KB |
Output is correct |
32 |
Correct |
174 ms |
33876 KB |
Output is correct |
33 |
Correct |
194 ms |
55640 KB |
Output is correct |
34 |
Correct |
214 ms |
62340 KB |
Output is correct |
35 |
Correct |
209 ms |
65068 KB |
Output is correct |
36 |
Correct |
194 ms |
69656 KB |
Output is correct |
37 |
Correct |
200 ms |
74364 KB |
Output is correct |
38 |
Correct |
206 ms |
81176 KB |
Output is correct |
39 |
Correct |
192 ms |
87852 KB |
Output is correct |
40 |
Correct |
246 ms |
94648 KB |
Output is correct |
41 |
Correct |
213 ms |
101244 KB |
Output is correct |
42 |
Correct |
239 ms |
108060 KB |
Output is correct |
43 |
Correct |
167 ms |
114732 KB |
Output is correct |
44 |
Incorrect |
2 ms |
114732 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |