#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,sse4")
#define pii pair<int,int>
#define ll long long
const int mxn = 1e6+10;
const int LEN = mxn*20;
int N;
int nid[LEN],to[LEN],wei[LEN],head[LEN];
int d1[LEN],d2[LEN];
ll ans = 0;
bitset<LEN> vis;
int ecnt,vcnt;
int seg[mxn*4];
void add_edge(int a,int b,int w){
//cerr<<"ADD_EDGE: "<<a<<' '<<b<<' '<<w<<endl;
ecnt++;
nid[ecnt] = head[a];
head[a] = ecnt;
to[ecnt] = b;
wei[ecnt] = w;
return;
}
void build(int now,int l,int r){
if(l == r){
seg[now] = l;
return;
}
seg[now] = vcnt++;
int mid = (l+r)>>1;
build(now*2+1,l,mid);
build(now*2+2,mid+1,r);
add_edge(seg[now*2+1],seg[now],0);
add_edge(seg[now*2+2],seg[now],0);
//cerr<<l<<' '<<r<<"::"<<seg[now]<<":"<<seg[now*2+1]<<' '<<seg[now*2+2]<<endl;
return;
}
void add(int now,int l,int r,int s,int e,int p){
if(l>=s&&e>=r){
//cerr<<l<<' '<<r<<":"<<p<<endl;
add_edge(seg[now],p,0);
return;
}
int mid = (l+r)>>1;
if(mid>=s)add(now*2+1,l,mid,s,e,p);
if(mid<e)add(now*2+2,mid+1,r,s,e,p);
return;
}
priority_queue<pii,vector<pii>,greater<pii>> pq;
void DIJKSTRA(int dist[]){
vis.reset();
for(int i = 0;i<vcnt;i++)pq.push(pii(dist[i],i));
while(!pq.empty()){
auto [d,now] = pq.top();
pq.pop();
if(vis[now])continue;
vis[now] = true;
for(int eid = head[now];eid;eid = nid[eid]){
int nxt = to[eid],w = wei[eid];
if(dist[nxt]>dist[now]+w){
dist[nxt] = dist[now]+w;
pq.push(pii(dist[nxt],nxt));
}
}
}
return;
}
long long minimum_walk(std::vector<int> p, int s) {
N = p.size();
vcnt = N;
for(int i = 0;i+1<N;i++){
add_edge(i,i+1,1);
add_edge(i+1,i,1);
}
build(0,0,N-1);
for(int i = 0;i<p.size();i++){
ans += abs(i-p[i]);
if(vis[i])continue;
int now = i;
int lp = i,rp = i;
do{
lp = min(lp,now);
rp = max(rp,now);
vis[now] = true;
now = p[now];
}while(now != i);
int tar = vcnt++;
add(0,0,N-1,lp,rp,tar);
do{
add_edge(tar,now,0);
now = p[now];
}while(now != i);
}
fill(d1,d1+LEN,LEN);
fill(d2,d2+LEN,LEN);
int tl = s,tr = s;
for(int i = 0;i<N;i++){
if(p[i] != i)tl = min(tl,i),tr = max(tr,i);
}
//cerr<<tl<<','<<tr<<endl;
d1[tl] = d2[tr] = 0;
DIJKSTRA(d1);
DIJKSTRA(d2);
//for(int i = 0;i<vcnt;i++)cerr<<d1[i]<<' ';cerr<<endl;
//for(int i = 0;i<vcnt;i++)cerr<<d2[i]<<' ';cerr<<endl;
for(int i = 0;i<vcnt;i++)d1[i] += d2[i];
DIJKSTRA(d1);
//for(int i = 0;i<vcnt;i++)cerr<<d1[i]<<' ';cerr<<endl;
return ans+d1[s]*2;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:85:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i = 0;i<p.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
166488 KB |
Output is correct |
2 |
Correct |
28 ms |
166484 KB |
Output is correct |
3 |
Correct |
29 ms |
166540 KB |
Output is correct |
4 |
Correct |
31 ms |
166584 KB |
Output is correct |
5 |
Correct |
33 ms |
166484 KB |
Output is correct |
6 |
Correct |
29 ms |
166484 KB |
Output is correct |
7 |
Correct |
28 ms |
166596 KB |
Output is correct |
8 |
Correct |
28 ms |
166640 KB |
Output is correct |
9 |
Correct |
28 ms |
166676 KB |
Output is correct |
10 |
Correct |
32 ms |
166604 KB |
Output is correct |
11 |
Correct |
28 ms |
166492 KB |
Output is correct |
12 |
Correct |
28 ms |
166504 KB |
Output is correct |
13 |
Correct |
28 ms |
166484 KB |
Output is correct |
14 |
Correct |
28 ms |
166492 KB |
Output is correct |
15 |
Correct |
28 ms |
166480 KB |
Output is correct |
16 |
Correct |
28 ms |
166492 KB |
Output is correct |
17 |
Correct |
30 ms |
166572 KB |
Output is correct |
18 |
Correct |
29 ms |
166656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
166488 KB |
Output is correct |
2 |
Correct |
28 ms |
166484 KB |
Output is correct |
3 |
Correct |
29 ms |
166540 KB |
Output is correct |
4 |
Correct |
31 ms |
166584 KB |
Output is correct |
5 |
Correct |
33 ms |
166484 KB |
Output is correct |
6 |
Correct |
29 ms |
166484 KB |
Output is correct |
7 |
Correct |
28 ms |
166596 KB |
Output is correct |
8 |
Correct |
28 ms |
166640 KB |
Output is correct |
9 |
Correct |
28 ms |
166676 KB |
Output is correct |
10 |
Correct |
32 ms |
166604 KB |
Output is correct |
11 |
Correct |
28 ms |
166492 KB |
Output is correct |
12 |
Correct |
28 ms |
166504 KB |
Output is correct |
13 |
Correct |
28 ms |
166484 KB |
Output is correct |
14 |
Correct |
28 ms |
166492 KB |
Output is correct |
15 |
Correct |
28 ms |
166480 KB |
Output is correct |
16 |
Correct |
28 ms |
166492 KB |
Output is correct |
17 |
Correct |
30 ms |
166572 KB |
Output is correct |
18 |
Correct |
29 ms |
166656 KB |
Output is correct |
19 |
Correct |
28 ms |
166740 KB |
Output is correct |
20 |
Correct |
29 ms |
166740 KB |
Output is correct |
21 |
Correct |
30 ms |
166744 KB |
Output is correct |
22 |
Correct |
31 ms |
166576 KB |
Output is correct |
23 |
Correct |
30 ms |
166632 KB |
Output is correct |
24 |
Correct |
33 ms |
166624 KB |
Output is correct |
25 |
Correct |
28 ms |
166748 KB |
Output is correct |
26 |
Correct |
32 ms |
167000 KB |
Output is correct |
27 |
Correct |
32 ms |
166616 KB |
Output is correct |
28 |
Correct |
30 ms |
166748 KB |
Output is correct |
29 |
Correct |
30 ms |
166736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
166488 KB |
Output is correct |
2 |
Correct |
28 ms |
166484 KB |
Output is correct |
3 |
Correct |
29 ms |
166540 KB |
Output is correct |
4 |
Correct |
31 ms |
166584 KB |
Output is correct |
5 |
Correct |
33 ms |
166484 KB |
Output is correct |
6 |
Correct |
29 ms |
166484 KB |
Output is correct |
7 |
Correct |
28 ms |
166596 KB |
Output is correct |
8 |
Correct |
28 ms |
166640 KB |
Output is correct |
9 |
Correct |
28 ms |
166676 KB |
Output is correct |
10 |
Correct |
32 ms |
166604 KB |
Output is correct |
11 |
Correct |
28 ms |
166492 KB |
Output is correct |
12 |
Correct |
28 ms |
166504 KB |
Output is correct |
13 |
Correct |
28 ms |
166484 KB |
Output is correct |
14 |
Correct |
28 ms |
166492 KB |
Output is correct |
15 |
Correct |
28 ms |
166480 KB |
Output is correct |
16 |
Correct |
28 ms |
166492 KB |
Output is correct |
17 |
Correct |
30 ms |
166572 KB |
Output is correct |
18 |
Correct |
29 ms |
166656 KB |
Output is correct |
19 |
Correct |
28 ms |
166740 KB |
Output is correct |
20 |
Correct |
29 ms |
166740 KB |
Output is correct |
21 |
Correct |
30 ms |
166744 KB |
Output is correct |
22 |
Correct |
31 ms |
166576 KB |
Output is correct |
23 |
Correct |
30 ms |
166632 KB |
Output is correct |
24 |
Correct |
33 ms |
166624 KB |
Output is correct |
25 |
Correct |
28 ms |
166748 KB |
Output is correct |
26 |
Correct |
32 ms |
167000 KB |
Output is correct |
27 |
Correct |
32 ms |
166616 KB |
Output is correct |
28 |
Correct |
30 ms |
166748 KB |
Output is correct |
29 |
Correct |
30 ms |
166736 KB |
Output is correct |
30 |
Correct |
1272 ms |
281008 KB |
Output is correct |
31 |
Correct |
1358 ms |
281184 KB |
Output is correct |
32 |
Correct |
1486 ms |
295340 KB |
Output is correct |
33 |
Correct |
1208 ms |
294068 KB |
Output is correct |
34 |
Correct |
1180 ms |
293992 KB |
Output is correct |
35 |
Correct |
1202 ms |
293556 KB |
Output is correct |
36 |
Correct |
1103 ms |
288184 KB |
Output is correct |
37 |
Correct |
1032 ms |
268988 KB |
Output is correct |
38 |
Correct |
937 ms |
264564 KB |
Output is correct |
39 |
Correct |
940 ms |
264636 KB |
Output is correct |
40 |
Correct |
955 ms |
264636 KB |
Output is correct |
41 |
Correct |
1153 ms |
281012 KB |
Output is correct |
42 |
Correct |
1213 ms |
281172 KB |
Output is correct |
43 |
Execution timed out |
2079 ms |
304568 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
166748 KB |
Output is correct |
2 |
Correct |
30 ms |
166748 KB |
Output is correct |
3 |
Correct |
29 ms |
166552 KB |
Output is correct |
4 |
Correct |
32 ms |
166748 KB |
Output is correct |
5 |
Correct |
31 ms |
166740 KB |
Output is correct |
6 |
Correct |
31 ms |
166748 KB |
Output is correct |
7 |
Correct |
29 ms |
166748 KB |
Output is correct |
8 |
Correct |
32 ms |
166740 KB |
Output is correct |
9 |
Correct |
29 ms |
166816 KB |
Output is correct |
10 |
Correct |
29 ms |
166748 KB |
Output is correct |
11 |
Correct |
33 ms |
166548 KB |
Output is correct |
12 |
Correct |
29 ms |
166740 KB |
Output is correct |
13 |
Correct |
29 ms |
166724 KB |
Output is correct |
14 |
Correct |
29 ms |
166744 KB |
Output is correct |
15 |
Correct |
30 ms |
166732 KB |
Output is correct |
16 |
Correct |
29 ms |
166736 KB |
Output is correct |
17 |
Correct |
30 ms |
166740 KB |
Output is correct |
18 |
Correct |
30 ms |
166604 KB |
Output is correct |
19 |
Correct |
29 ms |
166684 KB |
Output is correct |
20 |
Correct |
30 ms |
166720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
166488 KB |
Output is correct |
2 |
Correct |
28 ms |
166484 KB |
Output is correct |
3 |
Correct |
29 ms |
166540 KB |
Output is correct |
4 |
Correct |
31 ms |
166584 KB |
Output is correct |
5 |
Correct |
33 ms |
166484 KB |
Output is correct |
6 |
Correct |
29 ms |
166484 KB |
Output is correct |
7 |
Correct |
28 ms |
166596 KB |
Output is correct |
8 |
Correct |
28 ms |
166640 KB |
Output is correct |
9 |
Correct |
28 ms |
166676 KB |
Output is correct |
10 |
Correct |
32 ms |
166604 KB |
Output is correct |
11 |
Correct |
28 ms |
166492 KB |
Output is correct |
12 |
Correct |
28 ms |
166504 KB |
Output is correct |
13 |
Correct |
28 ms |
166484 KB |
Output is correct |
14 |
Correct |
28 ms |
166492 KB |
Output is correct |
15 |
Correct |
28 ms |
166480 KB |
Output is correct |
16 |
Correct |
28 ms |
166492 KB |
Output is correct |
17 |
Correct |
30 ms |
166572 KB |
Output is correct |
18 |
Correct |
29 ms |
166656 KB |
Output is correct |
19 |
Correct |
28 ms |
166740 KB |
Output is correct |
20 |
Correct |
29 ms |
166740 KB |
Output is correct |
21 |
Correct |
30 ms |
166744 KB |
Output is correct |
22 |
Correct |
31 ms |
166576 KB |
Output is correct |
23 |
Correct |
30 ms |
166632 KB |
Output is correct |
24 |
Correct |
33 ms |
166624 KB |
Output is correct |
25 |
Correct |
28 ms |
166748 KB |
Output is correct |
26 |
Correct |
32 ms |
167000 KB |
Output is correct |
27 |
Correct |
32 ms |
166616 KB |
Output is correct |
28 |
Correct |
30 ms |
166748 KB |
Output is correct |
29 |
Correct |
30 ms |
166736 KB |
Output is correct |
30 |
Correct |
1272 ms |
281008 KB |
Output is correct |
31 |
Correct |
1358 ms |
281184 KB |
Output is correct |
32 |
Correct |
1486 ms |
295340 KB |
Output is correct |
33 |
Correct |
1208 ms |
294068 KB |
Output is correct |
34 |
Correct |
1180 ms |
293992 KB |
Output is correct |
35 |
Correct |
1202 ms |
293556 KB |
Output is correct |
36 |
Correct |
1103 ms |
288184 KB |
Output is correct |
37 |
Correct |
1032 ms |
268988 KB |
Output is correct |
38 |
Correct |
937 ms |
264564 KB |
Output is correct |
39 |
Correct |
940 ms |
264636 KB |
Output is correct |
40 |
Correct |
955 ms |
264636 KB |
Output is correct |
41 |
Correct |
1153 ms |
281012 KB |
Output is correct |
42 |
Correct |
1213 ms |
281172 KB |
Output is correct |
43 |
Execution timed out |
2079 ms |
304568 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |