#include<bits/stdc++.h>
#include "books.h"
#define X first
#define Y second
using namespace std;
typedef pair<int,int> pii;
const int maxn=2e6,inf=1e9;
long long ans;
int n,cnt,MIN[maxn],MAX[maxn],nex[maxn],mol[maxn],nl,nr,x,y;
bool mark[maxn],seg[maxn];
map<pii,int> dp;
pii P[maxn];
vector<int>vec;
void dfs(int v,int u)
{
mark[v]=true;
mol[v]=cnt;
MIN[cnt]=min(MIN[cnt],v),MAX[cnt]=max(MAX[cnt],v);
if(nex[v]!=u) dfs(nex[v],u);
}
int go(int l,int r)
{
if(l==0 && r==n-1) return 0;
if(dp[pii(l,r)]) return dp[pii(l,r)];
return dp[pii(l,r)]=min((l==0?inf:go(P[mol[l-1]].X,max(P[mol[l-1]].Y,r))),(r==n-1?inf:go(min(l,P[mol[r+1]].X),P[mol[r+1]].Y)))+2;
}
void ok(int k)
{
if(!seg[k])
nl=min(nl,MIN[k]),nr=max(nr,MAX[k]),vec.push_back(k);
else
nl=min(nl,P[k].X),x=min(x,P[k].X),nr=max(nr,P[k].Y),y=max(y,P[k].Y);
}
void fmol(int k)
{
x=y=MIN[k];
vec.clear();vec.push_back(k);
nl=MIN[k],nr=MAX[k];
while(nl<x || y<nr)
{
if(nl<x) x--,ok(mol[x]);
else y++,ok(mol[y]);
}
for(int i=0;i<vec.size();i++)
P[vec[i]]=pii(x,y),seg[vec[i]]=true;
}
long long minimum_walk(vector<int> p, int s)
{
n=p.size();
for(int i=0;i<n;i++) nex[i]=p[i];
for(int i=0;i<n;i++)
{
ans+=abs(i-p[i]);
if(!mark[i]) {
MIN[cnt]=MAX[cnt]=i;
dfs(i,i);
cnt++;
}
}
if(ans==0) return 0;
for(int i=0;p[i]==i && i<s;i++)
ans-=2;
for(int i=n-1;p[i]==i && i>s;i--)
ans-=2;
for(int i=cnt-1;i>-1;i--)
if(!seg[i])
fmol(i);
ans+=go(P[mol[s]].X,P[mol[s]].Y);
return ans;
}
Compilation message
books.cpp: In function 'void fmol(int)':
books.cpp:49:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i=0;i<vec.size();i++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
109 ms |
27884 KB |
Output is correct |
31 |
Correct |
117 ms |
28020 KB |
Output is correct |
32 |
Correct |
84 ms |
31448 KB |
Output is correct |
33 |
Correct |
363 ms |
125304 KB |
Output is correct |
34 |
Correct |
332 ms |
125364 KB |
Output is correct |
35 |
Correct |
257 ms |
96980 KB |
Output is correct |
36 |
Correct |
134 ms |
46720 KB |
Output is correct |
37 |
Correct |
111 ms |
25232 KB |
Output is correct |
38 |
Correct |
89 ms |
23924 KB |
Output is correct |
39 |
Correct |
86 ms |
23932 KB |
Output is correct |
40 |
Correct |
117 ms |
24060 KB |
Output is correct |
41 |
Correct |
102 ms |
25840 KB |
Output is correct |
42 |
Correct |
126 ms |
25860 KB |
Output is correct |
43 |
Correct |
101 ms |
36472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
7024 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
12 ms |
2748 KB |
Output is correct |
4 |
Correct |
32 ms |
6652 KB |
Output is correct |
5 |
Correct |
30 ms |
6668 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
109 ms |
27884 KB |
Output is correct |
31 |
Correct |
117 ms |
28020 KB |
Output is correct |
32 |
Correct |
84 ms |
31448 KB |
Output is correct |
33 |
Correct |
363 ms |
125304 KB |
Output is correct |
34 |
Correct |
332 ms |
125364 KB |
Output is correct |
35 |
Correct |
257 ms |
96980 KB |
Output is correct |
36 |
Correct |
134 ms |
46720 KB |
Output is correct |
37 |
Correct |
111 ms |
25232 KB |
Output is correct |
38 |
Correct |
89 ms |
23924 KB |
Output is correct |
39 |
Correct |
86 ms |
23932 KB |
Output is correct |
40 |
Correct |
117 ms |
24060 KB |
Output is correct |
41 |
Correct |
102 ms |
25840 KB |
Output is correct |
42 |
Correct |
126 ms |
25860 KB |
Output is correct |
43 |
Correct |
101 ms |
36472 KB |
Output is correct |
44 |
Correct |
33 ms |
7024 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
12 ms |
2748 KB |
Output is correct |
47 |
Correct |
32 ms |
6652 KB |
Output is correct |
48 |
Correct |
30 ms |
6668 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
316 KB |
Output is correct |
54 |
Correct |
2 ms |
852 KB |
Output is correct |
55 |
Correct |
3 ms |
852 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
320 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
320 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
0 ms |
340 KB |
Output is correct |
64 |
Correct |
318 ms |
68632 KB |
Output is correct |
65 |
Execution timed out |
2072 ms |
252228 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |