#include "books.h"
#include <bits/stdc++.h>
#define MAXN 1000007
using namespace std;
bool vi[MAXN];
int a[MAXN],mik[MAXN],mak[MAXN],cnt;
long long d[1007][1007];
long long md[MAXN];
long long MST(int s)
{
for(int i=1;i<=cnt;i++) {vi[i]=false; md[i]=MAXN;}
md[s]=0;
long long t=0;
for(int x=0;x<cnt;x++)
{
pair<long long,int> mi=make_pair(MAXN,0);
for(int i=1;i<=cnt;i++) if(!vi[i]) mi=min(make_pair(md[i],i),mi);
int u=mi.second;
t+=mi.first;
vi[u]=true;
for(int i=1;i<=cnt;i++) if(!vi[i]) md[i]=min(md[i],d[u][i]);
}
return t;
}
long long minimum_walk(std::vector<int> p, int s)
{
if(s==0)
{
long long sk=0;
int mx=0;
for(int i=0;i<p.size();i++) if(!vi[i])
{
if(i>mx && p[i]!=i) sk+=2*((long long)i-mx);
int prev=i;
while(!vi[i])
{
if(p[i]!=i) mx=max(i,mx);
vi[i]=true;
i=p[i];
sk+=abs((long long)i-prev);
prev=i;
}
}
return sk;
}
long long sk=0;
for(int i=0;i<p.size();i++) if(!vi[i])
{
if(p[i]==i) continue;
int prev=i;
++cnt;
mik[cnt]=mak[cnt]=i;
while(!vi[i])
{
a[i]=cnt;
mik[cnt]=min(mik[cnt],i);
mak[cnt]=max(mak[cnt],i);
vi[i]=true;
i=p[i];
sk+=abs((long long)i-prev);
prev=i;
}
}
if(p[s]==s) {a[s]=++cnt; mak[cnt]=mik[cnt]=s;}
for(int i=1;i<=cnt;i++) for(int j=0;j<p.size();j++)
{
if(j>=mik[cnt] && j<=mak[cnt]) d[cnt][a[j]]=0;
else d[cnt][a[j]]=min(d[cnt][a[j]],max((long long)mik[cnt]-j,(long long)j-mak[cnt]));
}
return sk+2LL*MST(a[s]);
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:31:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<p.size();i++) if(!vi[i])
~^~~~~~~~~
books.cpp:47:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<p.size();i++) if(!vi[i])
~^~~~~~~~~
books.cpp:65:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<=cnt;i++) for(int j=0;j<p.size();j++)
~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
4 ms |
484 KB |
Output is correct |
5 |
Correct |
3 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
484 KB |
Output is correct |
7 |
Correct |
2 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
484 KB |
Output is correct |
9 |
Correct |
3 ms |
516 KB |
Output is correct |
10 |
Correct |
4 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
3 ms |
620 KB |
Output is correct |
17 |
Correct |
3 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
4 ms |
484 KB |
Output is correct |
5 |
Correct |
3 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
484 KB |
Output is correct |
7 |
Correct |
2 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
484 KB |
Output is correct |
9 |
Correct |
3 ms |
516 KB |
Output is correct |
10 |
Correct |
4 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
3 ms |
620 KB |
Output is correct |
17 |
Correct |
3 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
3 ms |
620 KB |
Output is correct |
23 |
Correct |
4 ms |
620 KB |
Output is correct |
24 |
Correct |
3 ms |
620 KB |
Output is correct |
25 |
Correct |
3 ms |
620 KB |
Output is correct |
26 |
Correct |
4 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
3 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
4 ms |
484 KB |
Output is correct |
5 |
Correct |
3 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
484 KB |
Output is correct |
7 |
Correct |
2 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
484 KB |
Output is correct |
9 |
Correct |
3 ms |
516 KB |
Output is correct |
10 |
Correct |
4 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
3 ms |
620 KB |
Output is correct |
17 |
Correct |
3 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
3 ms |
620 KB |
Output is correct |
23 |
Correct |
4 ms |
620 KB |
Output is correct |
24 |
Correct |
3 ms |
620 KB |
Output is correct |
25 |
Correct |
3 ms |
620 KB |
Output is correct |
26 |
Correct |
4 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
3 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
232 ms |
9400 KB |
Output is correct |
31 |
Correct |
312 ms |
9400 KB |
Output is correct |
32 |
Correct |
167 ms |
9400 KB |
Output is correct |
33 |
Correct |
172 ms |
9400 KB |
Output is correct |
34 |
Correct |
178 ms |
9452 KB |
Output is correct |
35 |
Correct |
183 ms |
9452 KB |
Output is correct |
36 |
Correct |
174 ms |
9452 KB |
Output is correct |
37 |
Correct |
171 ms |
9452 KB |
Output is correct |
38 |
Correct |
183 ms |
9452 KB |
Output is correct |
39 |
Correct |
166 ms |
9452 KB |
Output is correct |
40 |
Correct |
188 ms |
9452 KB |
Output is correct |
41 |
Correct |
246 ms |
9452 KB |
Output is correct |
42 |
Correct |
195 ms |
9452 KB |
Output is correct |
43 |
Correct |
168 ms |
9528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
9528 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 |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
4 ms |
484 KB |
Output is correct |
5 |
Correct |
3 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
484 KB |
Output is correct |
7 |
Correct |
2 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
484 KB |
Output is correct |
9 |
Correct |
3 ms |
516 KB |
Output is correct |
10 |
Correct |
4 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
3 ms |
620 KB |
Output is correct |
17 |
Correct |
3 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
3 ms |
620 KB |
Output is correct |
23 |
Correct |
4 ms |
620 KB |
Output is correct |
24 |
Correct |
3 ms |
620 KB |
Output is correct |
25 |
Correct |
3 ms |
620 KB |
Output is correct |
26 |
Correct |
4 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
3 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
232 ms |
9400 KB |
Output is correct |
31 |
Correct |
312 ms |
9400 KB |
Output is correct |
32 |
Correct |
167 ms |
9400 KB |
Output is correct |
33 |
Correct |
172 ms |
9400 KB |
Output is correct |
34 |
Correct |
178 ms |
9452 KB |
Output is correct |
35 |
Correct |
183 ms |
9452 KB |
Output is correct |
36 |
Correct |
174 ms |
9452 KB |
Output is correct |
37 |
Correct |
171 ms |
9452 KB |
Output is correct |
38 |
Correct |
183 ms |
9452 KB |
Output is correct |
39 |
Correct |
166 ms |
9452 KB |
Output is correct |
40 |
Correct |
188 ms |
9452 KB |
Output is correct |
41 |
Correct |
246 ms |
9452 KB |
Output is correct |
42 |
Correct |
195 ms |
9452 KB |
Output is correct |
43 |
Correct |
168 ms |
9528 KB |
Output is correct |
44 |
Incorrect |
3 ms |
9528 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |