#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define sz(v) ((int)(v).size())
typedef long long lint;
int n,sz;
int v[1000002];
int d[1000002];
vector<int>go;
vector<int>cycle[1000002];
lint cost[1000002];
void dfs(int p,int par)
{
cycle[sz].push_back(p);
cost[sz]+=abs(par-p);
v[p]=1;
if(!v[go[p]])dfs(go[p],p);
else cost[sz]+=abs(go[p]-p);
}
lint minimum_walk(vector<int> p, int s) {
n=sz(p);
go=p;
for(int i=0;i<n;i++)
{
if(!v[i])
{
sz++;
dfs(i,i);
}
}
vector<int>X;
lint ans=0;
int maxi=-1;
for(int i=1;i<=sz;i++)
{
ans+=cost[i];
sort(cycle[i].begin(),cycle[i].end());
d[cycle[i][0]]++;
d[cycle[i][sz(cycle[i])-1]]--;
if(sz(cycle[i])>1)maxi=max(maxi,cycle[i][sz(cycle[i])-1]-1);
}
int c=0;
for(int i=0;i<=maxi;i++)
{
c+=d[i];
if(c==0)ans+=2;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23708 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23692 KB |
Output is correct |
6 |
Correct |
11 ms |
23704 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23800 KB |
Output is correct |
9 |
Correct |
14 ms |
23784 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23760 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
12 ms |
23796 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23704 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23708 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23692 KB |
Output is correct |
6 |
Correct |
11 ms |
23704 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23800 KB |
Output is correct |
9 |
Correct |
14 ms |
23784 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23760 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
12 ms |
23796 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23704 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23800 KB |
Output is correct |
19 |
Correct |
12 ms |
23768 KB |
Output is correct |
20 |
Correct |
13 ms |
23848 KB |
Output is correct |
21 |
Correct |
13 ms |
23752 KB |
Output is correct |
22 |
Correct |
12 ms |
23820 KB |
Output is correct |
23 |
Correct |
11 ms |
23856 KB |
Output is correct |
24 |
Correct |
13 ms |
23852 KB |
Output is correct |
25 |
Correct |
12 ms |
23800 KB |
Output is correct |
26 |
Correct |
13 ms |
23776 KB |
Output is correct |
27 |
Correct |
13 ms |
23764 KB |
Output is correct |
28 |
Correct |
16 ms |
23756 KB |
Output is correct |
29 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23708 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23692 KB |
Output is correct |
6 |
Correct |
11 ms |
23704 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23800 KB |
Output is correct |
9 |
Correct |
14 ms |
23784 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23760 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
12 ms |
23796 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23704 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23800 KB |
Output is correct |
19 |
Correct |
12 ms |
23768 KB |
Output is correct |
20 |
Correct |
13 ms |
23848 KB |
Output is correct |
21 |
Correct |
13 ms |
23752 KB |
Output is correct |
22 |
Correct |
12 ms |
23820 KB |
Output is correct |
23 |
Correct |
11 ms |
23856 KB |
Output is correct |
24 |
Correct |
13 ms |
23852 KB |
Output is correct |
25 |
Correct |
12 ms |
23800 KB |
Output is correct |
26 |
Correct |
13 ms |
23776 KB |
Output is correct |
27 |
Correct |
13 ms |
23764 KB |
Output is correct |
28 |
Correct |
16 ms |
23756 KB |
Output is correct |
29 |
Correct |
13 ms |
23764 KB |
Output is correct |
30 |
Correct |
285 ms |
75516 KB |
Output is correct |
31 |
Correct |
239 ms |
63116 KB |
Output is correct |
32 |
Correct |
200 ms |
89140 KB |
Output is correct |
33 |
Correct |
192 ms |
77356 KB |
Output is correct |
34 |
Correct |
213 ms |
77320 KB |
Output is correct |
35 |
Correct |
190 ms |
72132 KB |
Output is correct |
36 |
Correct |
178 ms |
62636 KB |
Output is correct |
37 |
Correct |
168 ms |
56504 KB |
Output is correct |
38 |
Correct |
177 ms |
55612 KB |
Output is correct |
39 |
Correct |
190 ms |
55312 KB |
Output is correct |
40 |
Correct |
191 ms |
53540 KB |
Output is correct |
41 |
Correct |
226 ms |
61384 KB |
Output is correct |
42 |
Correct |
259 ms |
56036 KB |
Output is correct |
43 |
Correct |
182 ms |
79084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
23764 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23708 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23692 KB |
Output is correct |
6 |
Correct |
11 ms |
23704 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23800 KB |
Output is correct |
9 |
Correct |
14 ms |
23784 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23760 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
12 ms |
23796 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23704 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23800 KB |
Output is correct |
19 |
Correct |
12 ms |
23768 KB |
Output is correct |
20 |
Correct |
13 ms |
23848 KB |
Output is correct |
21 |
Correct |
13 ms |
23752 KB |
Output is correct |
22 |
Correct |
12 ms |
23820 KB |
Output is correct |
23 |
Correct |
11 ms |
23856 KB |
Output is correct |
24 |
Correct |
13 ms |
23852 KB |
Output is correct |
25 |
Correct |
12 ms |
23800 KB |
Output is correct |
26 |
Correct |
13 ms |
23776 KB |
Output is correct |
27 |
Correct |
13 ms |
23764 KB |
Output is correct |
28 |
Correct |
16 ms |
23756 KB |
Output is correct |
29 |
Correct |
13 ms |
23764 KB |
Output is correct |
30 |
Correct |
285 ms |
75516 KB |
Output is correct |
31 |
Correct |
239 ms |
63116 KB |
Output is correct |
32 |
Correct |
200 ms |
89140 KB |
Output is correct |
33 |
Correct |
192 ms |
77356 KB |
Output is correct |
34 |
Correct |
213 ms |
77320 KB |
Output is correct |
35 |
Correct |
190 ms |
72132 KB |
Output is correct |
36 |
Correct |
178 ms |
62636 KB |
Output is correct |
37 |
Correct |
168 ms |
56504 KB |
Output is correct |
38 |
Correct |
177 ms |
55612 KB |
Output is correct |
39 |
Correct |
190 ms |
55312 KB |
Output is correct |
40 |
Correct |
191 ms |
53540 KB |
Output is correct |
41 |
Correct |
226 ms |
61384 KB |
Output is correct |
42 |
Correct |
259 ms |
56036 KB |
Output is correct |
43 |
Correct |
182 ms |
79084 KB |
Output is correct |
44 |
Incorrect |
14 ms |
23764 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |