#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 |
23704 KB |
Output is correct |
2 |
Correct |
15 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23708 KB |
Output is correct |
5 |
Correct |
15 ms |
23820 KB |
Output is correct |
6 |
Correct |
15 ms |
23716 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23836 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23800 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23800 KB |
Output is correct |
16 |
Correct |
12 ms |
23720 KB |
Output is correct |
17 |
Correct |
14 ms |
23720 KB |
Output is correct |
18 |
Correct |
14 ms |
23744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23704 KB |
Output is correct |
2 |
Correct |
15 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23708 KB |
Output is correct |
5 |
Correct |
15 ms |
23820 KB |
Output is correct |
6 |
Correct |
15 ms |
23716 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23836 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23800 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23800 KB |
Output is correct |
16 |
Correct |
12 ms |
23720 KB |
Output is correct |
17 |
Correct |
14 ms |
23720 KB |
Output is correct |
18 |
Correct |
14 ms |
23744 KB |
Output is correct |
19 |
Correct |
16 ms |
23772 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23788 KB |
Output is correct |
23 |
Correct |
13 ms |
23892 KB |
Output is correct |
24 |
Correct |
13 ms |
23764 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23764 KB |
Output is correct |
27 |
Correct |
13 ms |
23788 KB |
Output is correct |
28 |
Correct |
13 ms |
23756 KB |
Output is correct |
29 |
Correct |
13 ms |
23796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23704 KB |
Output is correct |
2 |
Correct |
15 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23708 KB |
Output is correct |
5 |
Correct |
15 ms |
23820 KB |
Output is correct |
6 |
Correct |
15 ms |
23716 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23836 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23800 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23800 KB |
Output is correct |
16 |
Correct |
12 ms |
23720 KB |
Output is correct |
17 |
Correct |
14 ms |
23720 KB |
Output is correct |
18 |
Correct |
14 ms |
23744 KB |
Output is correct |
19 |
Correct |
16 ms |
23772 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23788 KB |
Output is correct |
23 |
Correct |
13 ms |
23892 KB |
Output is correct |
24 |
Correct |
13 ms |
23764 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23764 KB |
Output is correct |
27 |
Correct |
13 ms |
23788 KB |
Output is correct |
28 |
Correct |
13 ms |
23756 KB |
Output is correct |
29 |
Correct |
13 ms |
23796 KB |
Output is correct |
30 |
Correct |
253 ms |
72936 KB |
Output is correct |
31 |
Correct |
273 ms |
60460 KB |
Output is correct |
32 |
Correct |
173 ms |
90448 KB |
Output is correct |
33 |
Correct |
180 ms |
76160 KB |
Output is correct |
34 |
Correct |
183 ms |
76172 KB |
Output is correct |
35 |
Correct |
168 ms |
69860 KB |
Output is correct |
36 |
Correct |
191 ms |
58084 KB |
Output is correct |
37 |
Correct |
154 ms |
50284 KB |
Output is correct |
38 |
Correct |
165 ms |
49396 KB |
Output is correct |
39 |
Correct |
197 ms |
49492 KB |
Output is correct |
40 |
Correct |
175 ms |
49516 KB |
Output is correct |
41 |
Correct |
232 ms |
58492 KB |
Output is correct |
42 |
Correct |
219 ms |
52928 KB |
Output is correct |
43 |
Correct |
176 ms |
78540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
341 ms |
23860 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23704 KB |
Output is correct |
2 |
Correct |
15 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23708 KB |
Output is correct |
5 |
Correct |
15 ms |
23820 KB |
Output is correct |
6 |
Correct |
15 ms |
23716 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23836 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23800 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23800 KB |
Output is correct |
16 |
Correct |
12 ms |
23720 KB |
Output is correct |
17 |
Correct |
14 ms |
23720 KB |
Output is correct |
18 |
Correct |
14 ms |
23744 KB |
Output is correct |
19 |
Correct |
16 ms |
23772 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23788 KB |
Output is correct |
23 |
Correct |
13 ms |
23892 KB |
Output is correct |
24 |
Correct |
13 ms |
23764 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23764 KB |
Output is correct |
27 |
Correct |
13 ms |
23788 KB |
Output is correct |
28 |
Correct |
13 ms |
23756 KB |
Output is correct |
29 |
Correct |
13 ms |
23796 KB |
Output is correct |
30 |
Correct |
253 ms |
72936 KB |
Output is correct |
31 |
Correct |
273 ms |
60460 KB |
Output is correct |
32 |
Correct |
173 ms |
90448 KB |
Output is correct |
33 |
Correct |
180 ms |
76160 KB |
Output is correct |
34 |
Correct |
183 ms |
76172 KB |
Output is correct |
35 |
Correct |
168 ms |
69860 KB |
Output is correct |
36 |
Correct |
191 ms |
58084 KB |
Output is correct |
37 |
Correct |
154 ms |
50284 KB |
Output is correct |
38 |
Correct |
165 ms |
49396 KB |
Output is correct |
39 |
Correct |
197 ms |
49492 KB |
Output is correct |
40 |
Correct |
175 ms |
49516 KB |
Output is correct |
41 |
Correct |
232 ms |
58492 KB |
Output is correct |
42 |
Correct |
219 ms |
52928 KB |
Output is correct |
43 |
Correct |
176 ms |
78540 KB |
Output is correct |
44 |
Incorrect |
341 ms |
23860 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
45 |
Halted |
0 ms |
0 KB |
- |