#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];
int le[1000002],ri[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,mini=1e9;
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);
mini=min(mini,cycle[i][0]);
if(cycle[i][0]<s && s<cycle[i][sz(cycle[i])-1])
{
le[i]=-1e9;ri[i]=1e9;
for(auto &k:cycle[i])
{
if(k<=s)le[i]=max(le[i],k);
if(k>=s)ri[i]=min(ri[i],k);
}
}
}
else le[i]=ri[i]=s;
}/*
if(s!=0)
{
int cnt=0,tmp=1e9;
for(int i=0;i<=s;i++)
{
for(int j=s;j<n;j++)
{
for(int k=1;k<=sz;k++)
{
if(le[k]>=i || ri[k]<=j)cnt++;
}
if(cnt==sz)
{
tmp=min(tmp,j-i);
}
}
}
ans+=2*tmp;
}*/
int c=0;
for(int i=0;i<n;i++)
{
c+=d[i];
d[i]=c;
}
if(s<=mini)
{
for(int i=s;i<=maxi;i++)
{
if(!d[i])ans+=2;
}
}
else if(s>maxi)
{
for(int i=mini;i<s;i++)
{
if(!d[i])ans+=2;
}
}
else
{
for(int i=mini;i<=maxi;i++)
{
if(!d[i])ans+=2;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23716 KB |
Output is correct |
6 |
Correct |
12 ms |
23816 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23760 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23716 KB |
Output is correct |
6 |
Correct |
12 ms |
23816 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23760 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23736 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
13 ms |
23796 KB |
Output is correct |
23 |
Correct |
14 ms |
23760 KB |
Output is correct |
24 |
Correct |
13 ms |
23756 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23864 KB |
Output is correct |
27 |
Correct |
13 ms |
23764 KB |
Output is correct |
28 |
Correct |
14 ms |
23740 KB |
Output is correct |
29 |
Correct |
16 ms |
23804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23716 KB |
Output is correct |
6 |
Correct |
12 ms |
23816 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23760 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23736 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
13 ms |
23796 KB |
Output is correct |
23 |
Correct |
14 ms |
23760 KB |
Output is correct |
24 |
Correct |
13 ms |
23756 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23864 KB |
Output is correct |
27 |
Correct |
13 ms |
23764 KB |
Output is correct |
28 |
Correct |
14 ms |
23740 KB |
Output is correct |
29 |
Correct |
16 ms |
23804 KB |
Output is correct |
30 |
Correct |
258 ms |
72640 KB |
Output is correct |
31 |
Correct |
271 ms |
60220 KB |
Output is correct |
32 |
Correct |
170 ms |
90296 KB |
Output is correct |
33 |
Correct |
192 ms |
75960 KB |
Output is correct |
34 |
Correct |
184 ms |
75968 KB |
Output is correct |
35 |
Correct |
174 ms |
69692 KB |
Output is correct |
36 |
Correct |
174 ms |
58048 KB |
Output is correct |
37 |
Correct |
198 ms |
50044 KB |
Output is correct |
38 |
Correct |
169 ms |
49276 KB |
Output is correct |
39 |
Correct |
158 ms |
49120 KB |
Output is correct |
40 |
Correct |
175 ms |
49224 KB |
Output is correct |
41 |
Correct |
198 ms |
58312 KB |
Output is correct |
42 |
Correct |
195 ms |
52760 KB |
Output is correct |
43 |
Correct |
168 ms |
78148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
23768 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 |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23716 KB |
Output is correct |
6 |
Correct |
12 ms |
23816 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23760 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23736 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
13 ms |
23796 KB |
Output is correct |
23 |
Correct |
14 ms |
23760 KB |
Output is correct |
24 |
Correct |
13 ms |
23756 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23864 KB |
Output is correct |
27 |
Correct |
13 ms |
23764 KB |
Output is correct |
28 |
Correct |
14 ms |
23740 KB |
Output is correct |
29 |
Correct |
16 ms |
23804 KB |
Output is correct |
30 |
Correct |
258 ms |
72640 KB |
Output is correct |
31 |
Correct |
271 ms |
60220 KB |
Output is correct |
32 |
Correct |
170 ms |
90296 KB |
Output is correct |
33 |
Correct |
192 ms |
75960 KB |
Output is correct |
34 |
Correct |
184 ms |
75968 KB |
Output is correct |
35 |
Correct |
174 ms |
69692 KB |
Output is correct |
36 |
Correct |
174 ms |
58048 KB |
Output is correct |
37 |
Correct |
198 ms |
50044 KB |
Output is correct |
38 |
Correct |
169 ms |
49276 KB |
Output is correct |
39 |
Correct |
158 ms |
49120 KB |
Output is correct |
40 |
Correct |
175 ms |
49224 KB |
Output is correct |
41 |
Correct |
198 ms |
58312 KB |
Output is correct |
42 |
Correct |
195 ms |
52760 KB |
Output is correct |
43 |
Correct |
168 ms |
78148 KB |
Output is correct |
44 |
Incorrect |
16 ms |
23768 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |