#include "books.h"
#define N 1000005
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans = 0;
ll n, maior[N], qtd = 0, ok[N], en;
vector<int> g;
void dfs(ll x, ll &mx)
{
mx = max(mx, maior[x]);
if(ok[x]) return;
ok[x] = 1;
dfs(g[x], mx);
}
ll minimum_walk(vector<int> p, int s)
{
n = p.size();
g = p;
for(ll i = 0; i < n; i++) ans += abs(i - p[i]);
for(int i = 0; i < n; i++) maior[i] = i;
for(ll i = 0; i < n; i++) dfs(i, maior[i]);
for(ll i = n - 1; i >= 0; i--)
{
if(p[i] != i)
{
en = i;
break;
}
}
ll pos = 0, qtd = 0;
while(pos <= en)
{
pos = maior[pos] + 1;
qtd ++;
}
return ans + 2*(qtd - 1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
2 ms |
608 KB |
Output is correct |
9 |
Correct |
2 ms |
608 KB |
Output is correct |
10 |
Correct |
2 ms |
608 KB |
Output is correct |
11 |
Correct |
2 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
608 KB |
Output is correct |
13 |
Correct |
2 ms |
608 KB |
Output is correct |
14 |
Correct |
2 ms |
608 KB |
Output is correct |
15 |
Correct |
2 ms |
608 KB |
Output is correct |
16 |
Correct |
2 ms |
608 KB |
Output is correct |
17 |
Correct |
2 ms |
608 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
2 ms |
608 KB |
Output is correct |
9 |
Correct |
2 ms |
608 KB |
Output is correct |
10 |
Correct |
2 ms |
608 KB |
Output is correct |
11 |
Correct |
2 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
608 KB |
Output is correct |
13 |
Correct |
2 ms |
608 KB |
Output is correct |
14 |
Correct |
2 ms |
608 KB |
Output is correct |
15 |
Correct |
2 ms |
608 KB |
Output is correct |
16 |
Correct |
2 ms |
608 KB |
Output is correct |
17 |
Correct |
2 ms |
608 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Incorrect |
2 ms |
624 KB |
3rd lines differ - on the 1st token, expected: '338572', found: '338578' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
2 ms |
608 KB |
Output is correct |
9 |
Correct |
2 ms |
608 KB |
Output is correct |
10 |
Correct |
2 ms |
608 KB |
Output is correct |
11 |
Correct |
2 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
608 KB |
Output is correct |
13 |
Correct |
2 ms |
608 KB |
Output is correct |
14 |
Correct |
2 ms |
608 KB |
Output is correct |
15 |
Correct |
2 ms |
608 KB |
Output is correct |
16 |
Correct |
2 ms |
608 KB |
Output is correct |
17 |
Correct |
2 ms |
608 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Incorrect |
2 ms |
624 KB |
3rd lines differ - on the 1st token, expected: '338572', found: '338578' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
624 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
2 ms |
608 KB |
Output is correct |
9 |
Correct |
2 ms |
608 KB |
Output is correct |
10 |
Correct |
2 ms |
608 KB |
Output is correct |
11 |
Correct |
2 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
608 KB |
Output is correct |
13 |
Correct |
2 ms |
608 KB |
Output is correct |
14 |
Correct |
2 ms |
608 KB |
Output is correct |
15 |
Correct |
2 ms |
608 KB |
Output is correct |
16 |
Correct |
2 ms |
608 KB |
Output is correct |
17 |
Correct |
2 ms |
608 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Incorrect |
2 ms |
624 KB |
3rd lines differ - on the 1st token, expected: '338572', found: '338578' |
20 |
Halted |
0 ms |
0 KB |
- |