#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
#define INF 1000000000000000000LL
#define ff first
#define ss second
lld n;
lld comps;
lld cyc[1000100];
vector<vector<lld>> cycles;
pair<pair<lld,lld>,pair<lld,lld>> reach[1000100];
lld lfinal, rfinal;
void debug(){
for(lld i=0;i<n;i++) cout << cyc[i] << " \n"[i==n-1];
for(lld i=0;i<comps;i++)
if((lld)cycles[i].size()>1) cout << i << ": " << reach[i].ff.ff << " " << reach[i].ff.ss << " " << reach[i].ss.ff << " " << reach[i].ss.ss << "\n";
}
lld minimum_walk(vector<int> p, int s){
n = (lld) p.size();
lld ans = 0;
for(lld i=0;i<n;i++) ans+=abs(p[i]-i);
vector<bool> vis(n, false);
comps = 0;
lfinal = INF, rfinal = -1;
for(lld i=0;i<n;i++){
if(!vis[i]){
vector<lld> tmp;
lld x = i;
while(true){
if(vis[x]) break;
tmp.push_back(x);
vis[x] = true;
cyc[x] = comps;
x = p[x];
}
sort(tmp.begin(), tmp.end());
cycles.push_back(tmp);
if((int)tmp.size()>1){
if(tmp.front()>=s) reach[comps] = {{tmp.front(),-1},{tmp.front(),tmp.back()}};
else if(tmp.back()<=s) reach[comps] = {{tmp.front(),tmp.back()},{INF,tmp.back()}};
else{
auto it = lower_bound(tmp.begin(), tmp.end(), s);
reach[comps] = {{tmp.front(),*prev(it)},{*it,tmp.back()}};
}
lfinal = min(lfinal, reach[comps].ff.ff);
rfinal = max(rfinal, reach[comps].ss.ss);
}else{
reach[comps] = {{tmp[0],tmp[0]},{tmp[0],tmp[0]}};
}
comps++;
}
}
lld cur_comp = cyc[s];
lld lblock = s, rblock = s;
lld lblocknew = reach[cur_comp].ff.ff, rblocknew = reach[cur_comp].ss.ss;
while(lblocknew!=lblock||rblocknew!=rblock){
lld lblocknewnew, rblocknewnew;
for(lld i=lblocknew;i<=lblock;i++){
if((lld)cycles[cyc[i]].size()==1) continue;
reach[cur_comp].ff.ff = min(reach[cur_comp].ff.ff, reach[cyc[i]].ff.ff);
reach[cur_comp].ff.ss = max(reach[cur_comp].ff.ss, reach[cyc[i]].ff.ss);
reach[cur_comp].ss.ff = min(reach[cur_comp].ss.ff, reach[cyc[i]].ss.ff);
reach[cur_comp].ss.ss = max(reach[cur_comp].ss.ss, reach[cyc[i]].ss.ss);
lblocknewnew = reach[cur_comp].ff.ff;
rblocknewnew = reach[cur_comp].ss.ss;
cyc[i] = cur_comp;
}
for(lld i=rblock;i<=rblocknew;i++){
if((lld)cycles[cyc[i]].size()==1) continue;
reach[cur_comp].ff.ff = min(reach[cur_comp].ff.ff, reach[cyc[i]].ff.ff);
reach[cur_comp].ff.ss = max(reach[cur_comp].ff.ss, reach[cyc[i]].ff.ss);
reach[cur_comp].ss.ff = min(reach[cur_comp].ss.ff, reach[cyc[i]].ss.ff);
reach[cur_comp].ss.ss = max(reach[cur_comp].ss.ss, reach[cyc[i]].ss.ss);
lblocknewnew = reach[cur_comp].ff.ff;
rblocknewnew = reach[cur_comp].ss.ss;
cyc[i] = cur_comp;
}
lblock = lblocknew; rblock = rblocknew;
lblocknew = lblocknewnew; rblocknew = rblocknewnew;
}
lld l = INF;
for(lld i=lblock-1;i>=lfinal;i--){
if((lld)cycles[cyc[i]].size()==1) continue;
if(i<l){
cur_comp = cyc[i];
l = reach[cur_comp].ff.ff;
}else{
reach[cur_comp].ff.ff = min(reach[cur_comp].ff.ff, reach[cyc[i]].ff.ff);
reach[cur_comp].ff.ss = max(reach[cur_comp].ff.ss, reach[cyc[i]].ff.ss);
reach[cur_comp].ss.ff = min(reach[cur_comp].ss.ff, reach[cyc[i]].ss.ff);
reach[cur_comp].ss.ss = max(reach[cur_comp].ss.ss, reach[cyc[i]].ss.ss);
l = reach[cur_comp].ff.ff;
cyc[i] = cur_comp;
}
}
lld r = -1;
for(lld i=rblock+1;i<=rfinal;i++){
if((lld)cycles[cyc[i]].size()==1) continue;
if(i>r){
cur_comp = cyc[i];
r = reach[cur_comp].ss.ss;
}else{
reach[cur_comp].ff.ff = min(reach[cur_comp].ff.ff, reach[cyc[i]].ff.ff);
reach[cur_comp].ff.ss = max(reach[cur_comp].ff.ss, reach[cyc[i]].ff.ss);
reach[cur_comp].ss.ff = min(reach[cur_comp].ss.ff, reach[cyc[i]].ss.ff);
reach[cur_comp].ss.ss = max(reach[cur_comp].ss.ss, reach[cyc[i]].ss.ss);
r = reach[cur_comp].ss.ss;
cyc[i] = cur_comp;
}
}
//debug();
l = lblock, r = rblock;
while(l>lfinal||r<rfinal){
lld i, j;
lld lpath = (l-1<lfinal?INF:0), rpath = (r+1>rfinal?INF:0);
for(i=l-1;i>=lfinal;i--){
if(reach[cyc[i]].ff.ff<s&&reach[cyc[i]].ss.ss>s){
lpath+=2;
break;
}
if(cyc[i]!=cyc[i+1]) lpath+=2;
i = reach[cyc[i]].ff.ff;
if(i==lfinal) break;
}
for(j=r+1;j<=rfinal;j++){
if(reach[cyc[j]].ff.ff<s&&reach[cyc[j]].ss.ss>s){
rpath+=2;
break;
}
if(cyc[j]!=cyc[j-1]) rpath+=2;
j = reach[cyc[j]].ss.ss;
if(j==rfinal) break;
}
ans += min(lpath, rpath);
l = reach[cyc[i]].ff.ff;
r = reach[cyc[j]].ss.ss;
}
return ans;
}
Compilation message
books.cpp: In function 'lld minimum_walk(std::vector<int>, int)':
books.cpp:60:36: warning: 'rblocknewnew' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | while(lblocknew!=lblock||rblocknew!=rblock){
| ~~~~~~~~~^~~~~~~~
books.cpp:60:17: warning: 'lblocknewnew' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | while(lblocknew!=lblock||rblocknew!=rblock){
| ~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
232 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
232 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
232 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
283 ms |
35068 KB |
Output is correct |
31 |
Correct |
249 ms |
32440 KB |
Output is correct |
32 |
Correct |
233 ms |
108904 KB |
Output is correct |
33 |
Correct |
199 ms |
82772 KB |
Output is correct |
34 |
Correct |
204 ms |
82932 KB |
Output is correct |
35 |
Correct |
217 ms |
81164 KB |
Output is correct |
36 |
Correct |
158 ms |
48932 KB |
Output is correct |
37 |
Correct |
145 ms |
33356 KB |
Output is correct |
38 |
Correct |
157 ms |
31092 KB |
Output is correct |
39 |
Correct |
161 ms |
30960 KB |
Output is correct |
40 |
Correct |
160 ms |
30712 KB |
Output is correct |
41 |
Correct |
205 ms |
31324 KB |
Output is correct |
42 |
Correct |
212 ms |
31432 KB |
Output is correct |
43 |
Correct |
205 ms |
86632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
316 KB |
3rd lines differ - on the 1st token, expected: '9800', found: '9802' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
232 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
283 ms |
35068 KB |
Output is correct |
31 |
Correct |
249 ms |
32440 KB |
Output is correct |
32 |
Correct |
233 ms |
108904 KB |
Output is correct |
33 |
Correct |
199 ms |
82772 KB |
Output is correct |
34 |
Correct |
204 ms |
82932 KB |
Output is correct |
35 |
Correct |
217 ms |
81164 KB |
Output is correct |
36 |
Correct |
158 ms |
48932 KB |
Output is correct |
37 |
Correct |
145 ms |
33356 KB |
Output is correct |
38 |
Correct |
157 ms |
31092 KB |
Output is correct |
39 |
Correct |
161 ms |
30960 KB |
Output is correct |
40 |
Correct |
160 ms |
30712 KB |
Output is correct |
41 |
Correct |
205 ms |
31324 KB |
Output is correct |
42 |
Correct |
212 ms |
31432 KB |
Output is correct |
43 |
Correct |
205 ms |
86632 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Incorrect |
1 ms |
316 KB |
3rd lines differ - on the 1st token, expected: '9800', found: '9802' |
46 |
Halted |
0 ms |
0 KB |
- |