#include <bits/stdc++.h>
#include "books.h"
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
using namespace std;
typedef long long ll;
const int maxN = 1e6 + 10;
bool mark[maxN];
vector <pii> que;
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
ll ans = 0;
for (int i=0; i<n; i++)
{
if(mark[i]) continue ;
int curr = i, mn = n, sz = 0, mx = 0;
while(!mark[curr])
{
mn = min(mn, curr);
mx = max(mx, curr);
sz ++;
mark[curr] = true;
ans += abs(p[curr] - curr);
curr = p[curr];
}
que.pb({mn, mx});
}
sort(que.begin(), que.end());
int len = que.size();
int maxR = 0, currL = 0;
vector <pii> res;
for (int i=0; i<len; i++)
{
if(maxR >= que[i].F) maxR = max(maxR, que[i].S);
else
{
res.pb({currL, maxR});
currL = maxR + 1;
maxR = que[i].S;
}
}
res.pb({currL, maxR});
int M = -1, m = -1, mid = -1;
for (int i=0; i<(int)res.size(); i++){
if(res[i].F <= s && s <= res[i].S) mid = i;
if(res[i].F == res[i].S) continue ;
if(res[i].S < s && m == -1) m = i;
if(res[i].F > s) M = i;
}
if(m == -1) m = mid;
if(M == -1) M = mid;
return ans + 2LL * (M - m);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
500 KB |
Output is correct |
7 |
Correct |
2 ms |
500 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
2 ms |
568 KB |
Output is correct |
10 |
Correct |
3 ms |
568 KB |
Output is correct |
11 |
Correct |
2 ms |
568 KB |
Output is correct |
12 |
Correct |
2 ms |
568 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
500 KB |
Output is correct |
7 |
Correct |
2 ms |
500 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
2 ms |
568 KB |
Output is correct |
10 |
Correct |
3 ms |
568 KB |
Output is correct |
11 |
Correct |
2 ms |
568 KB |
Output is correct |
12 |
Correct |
2 ms |
568 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
624 KB |
Output is correct |
22 |
Correct |
3 ms |
624 KB |
Output is correct |
23 |
Correct |
3 ms |
624 KB |
Output is correct |
24 |
Correct |
3 ms |
624 KB |
Output is correct |
25 |
Correct |
3 ms |
624 KB |
Output is correct |
26 |
Correct |
3 ms |
624 KB |
Output is correct |
27 |
Correct |
3 ms |
624 KB |
Output is correct |
28 |
Correct |
3 ms |
624 KB |
Output is correct |
29 |
Correct |
3 ms |
636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
500 KB |
Output is correct |
7 |
Correct |
2 ms |
500 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
2 ms |
568 KB |
Output is correct |
10 |
Correct |
3 ms |
568 KB |
Output is correct |
11 |
Correct |
2 ms |
568 KB |
Output is correct |
12 |
Correct |
2 ms |
568 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
624 KB |
Output is correct |
22 |
Correct |
3 ms |
624 KB |
Output is correct |
23 |
Correct |
3 ms |
624 KB |
Output is correct |
24 |
Correct |
3 ms |
624 KB |
Output is correct |
25 |
Correct |
3 ms |
624 KB |
Output is correct |
26 |
Correct |
3 ms |
624 KB |
Output is correct |
27 |
Correct |
3 ms |
624 KB |
Output is correct |
28 |
Correct |
3 ms |
624 KB |
Output is correct |
29 |
Correct |
3 ms |
636 KB |
Output is correct |
30 |
Correct |
334 ms |
9340 KB |
Output is correct |
31 |
Correct |
323 ms |
9468 KB |
Output is correct |
32 |
Correct |
229 ms |
29584 KB |
Output is correct |
33 |
Correct |
224 ms |
29584 KB |
Output is correct |
34 |
Correct |
236 ms |
29584 KB |
Output is correct |
35 |
Correct |
219 ms |
29584 KB |
Output is correct |
36 |
Correct |
257 ms |
29584 KB |
Output is correct |
37 |
Correct |
199 ms |
29584 KB |
Output is correct |
38 |
Correct |
147 ms |
29584 KB |
Output is correct |
39 |
Correct |
168 ms |
29584 KB |
Output is correct |
40 |
Correct |
224 ms |
29584 KB |
Output is correct |
41 |
Correct |
257 ms |
29584 KB |
Output is correct |
42 |
Correct |
191 ms |
29584 KB |
Output is correct |
43 |
Correct |
217 ms |
29584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
29584 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
500 KB |
Output is correct |
7 |
Correct |
2 ms |
500 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
2 ms |
568 KB |
Output is correct |
10 |
Correct |
3 ms |
568 KB |
Output is correct |
11 |
Correct |
2 ms |
568 KB |
Output is correct |
12 |
Correct |
2 ms |
568 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
624 KB |
Output is correct |
22 |
Correct |
3 ms |
624 KB |
Output is correct |
23 |
Correct |
3 ms |
624 KB |
Output is correct |
24 |
Correct |
3 ms |
624 KB |
Output is correct |
25 |
Correct |
3 ms |
624 KB |
Output is correct |
26 |
Correct |
3 ms |
624 KB |
Output is correct |
27 |
Correct |
3 ms |
624 KB |
Output is correct |
28 |
Correct |
3 ms |
624 KB |
Output is correct |
29 |
Correct |
3 ms |
636 KB |
Output is correct |
30 |
Correct |
334 ms |
9340 KB |
Output is correct |
31 |
Correct |
323 ms |
9468 KB |
Output is correct |
32 |
Correct |
229 ms |
29584 KB |
Output is correct |
33 |
Correct |
224 ms |
29584 KB |
Output is correct |
34 |
Correct |
236 ms |
29584 KB |
Output is correct |
35 |
Correct |
219 ms |
29584 KB |
Output is correct |
36 |
Correct |
257 ms |
29584 KB |
Output is correct |
37 |
Correct |
199 ms |
29584 KB |
Output is correct |
38 |
Correct |
147 ms |
29584 KB |
Output is correct |
39 |
Correct |
168 ms |
29584 KB |
Output is correct |
40 |
Correct |
224 ms |
29584 KB |
Output is correct |
41 |
Correct |
257 ms |
29584 KB |
Output is correct |
42 |
Correct |
191 ms |
29584 KB |
Output is correct |
43 |
Correct |
217 ms |
29584 KB |
Output is correct |
44 |
Incorrect |
3 ms |
29584 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |