#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,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])
{
int tmp=1e9;
for(auto &k:cycle[i])
{
tmp=min(tmp,abs(s-k));
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23692 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23796 KB |
Output is correct |
7 |
Correct |
12 ms |
23692 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23788 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23796 KB |
Output is correct |
12 |
Correct |
11 ms |
23804 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23704 KB |
Output is correct |
15 |
Correct |
12 ms |
23796 KB |
Output is correct |
16 |
Correct |
14 ms |
23704 KB |
Output is correct |
17 |
Correct |
12 ms |
23696 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23692 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23796 KB |
Output is correct |
7 |
Correct |
12 ms |
23692 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23788 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23796 KB |
Output is correct |
12 |
Correct |
11 ms |
23804 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23704 KB |
Output is correct |
15 |
Correct |
12 ms |
23796 KB |
Output is correct |
16 |
Correct |
14 ms |
23704 KB |
Output is correct |
17 |
Correct |
12 ms |
23696 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23804 KB |
Output is correct |
22 |
Correct |
13 ms |
23800 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23840 KB |
Output is correct |
25 |
Correct |
14 ms |
23800 KB |
Output is correct |
26 |
Correct |
13 ms |
23792 KB |
Output is correct |
27 |
Correct |
12 ms |
23796 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
29 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23692 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23796 KB |
Output is correct |
7 |
Correct |
12 ms |
23692 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23788 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23796 KB |
Output is correct |
12 |
Correct |
11 ms |
23804 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23704 KB |
Output is correct |
15 |
Correct |
12 ms |
23796 KB |
Output is correct |
16 |
Correct |
14 ms |
23704 KB |
Output is correct |
17 |
Correct |
12 ms |
23696 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23804 KB |
Output is correct |
22 |
Correct |
13 ms |
23800 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23840 KB |
Output is correct |
25 |
Correct |
14 ms |
23800 KB |
Output is correct |
26 |
Correct |
13 ms |
23792 KB |
Output is correct |
27 |
Correct |
12 ms |
23796 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
29 |
Correct |
12 ms |
23764 KB |
Output is correct |
30 |
Correct |
245 ms |
72804 KB |
Output is correct |
31 |
Correct |
262 ms |
60532 KB |
Output is correct |
32 |
Correct |
151 ms |
82672 KB |
Output is correct |
33 |
Correct |
159 ms |
70732 KB |
Output is correct |
34 |
Correct |
158 ms |
70772 KB |
Output is correct |
35 |
Correct |
158 ms |
65616 KB |
Output is correct |
36 |
Correct |
146 ms |
56064 KB |
Output is correct |
37 |
Correct |
143 ms |
49920 KB |
Output is correct |
38 |
Correct |
149 ms |
49412 KB |
Output is correct |
39 |
Correct |
153 ms |
49284 KB |
Output is correct |
40 |
Correct |
171 ms |
49548 KB |
Output is correct |
41 |
Correct |
207 ms |
58524 KB |
Output is correct |
42 |
Correct |
189 ms |
52852 KB |
Output is correct |
43 |
Correct |
162 ms |
72524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23824 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3592' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23692 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23796 KB |
Output is correct |
7 |
Correct |
12 ms |
23692 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23788 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23796 KB |
Output is correct |
12 |
Correct |
11 ms |
23804 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23704 KB |
Output is correct |
15 |
Correct |
12 ms |
23796 KB |
Output is correct |
16 |
Correct |
14 ms |
23704 KB |
Output is correct |
17 |
Correct |
12 ms |
23696 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23804 KB |
Output is correct |
22 |
Correct |
13 ms |
23800 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23840 KB |
Output is correct |
25 |
Correct |
14 ms |
23800 KB |
Output is correct |
26 |
Correct |
13 ms |
23792 KB |
Output is correct |
27 |
Correct |
12 ms |
23796 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
29 |
Correct |
12 ms |
23764 KB |
Output is correct |
30 |
Correct |
245 ms |
72804 KB |
Output is correct |
31 |
Correct |
262 ms |
60532 KB |
Output is correct |
32 |
Correct |
151 ms |
82672 KB |
Output is correct |
33 |
Correct |
159 ms |
70732 KB |
Output is correct |
34 |
Correct |
158 ms |
70772 KB |
Output is correct |
35 |
Correct |
158 ms |
65616 KB |
Output is correct |
36 |
Correct |
146 ms |
56064 KB |
Output is correct |
37 |
Correct |
143 ms |
49920 KB |
Output is correct |
38 |
Correct |
149 ms |
49412 KB |
Output is correct |
39 |
Correct |
153 ms |
49284 KB |
Output is correct |
40 |
Correct |
171 ms |
49548 KB |
Output is correct |
41 |
Correct |
207 ms |
58524 KB |
Output is correct |
42 |
Correct |
189 ms |
52852 KB |
Output is correct |
43 |
Correct |
162 ms |
72524 KB |
Output is correct |
44 |
Incorrect |
12 ms |
23824 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3592' |
45 |
Halted |
0 ms |
0 KB |
- |