#include "books.h"
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <cstdio>
#include <cstring>
using namespace std;
bool visit[1000100];
long long dis[1000100];
vector<pair<int,int>> uni(vector<pair<int,int>> &tbl) {
sort(tbl.begin(),tbl.end(),[](const pair<int,int> &a,const pair<int,int> &b) {
if (a.second!=b.second) return a.second<b.second;
return a.first<b.first;
});
vector<pair<int,int>> re;
for (int i=0;i<(int)tbl.size();i++) {
int curl=tbl[i].first;
while (!re.empty()&&tbl[i].first<=re.back().second) {
curl=min(curl,re.back().first);
re.pop_back();
}
re.push_back({curl,tbl[i].second});
}
return re;
}
struct seg {
int cl,cr,pl,pr;
};
vector<seg> rem(vector<seg> &tbl,vector<seg> &out) {
sort(tbl.begin(),tbl.end(),[](const seg &a, const seg &b) {
if (a.cr!=b.cr) return a.cr<b.cr;
return a.cl>b.cl;
});
vector<seg> res;
for (int i=0;i<(int)tbl.size();i++) {
while (!res.empty()&&tbl[i].cl<=res.back().cl) {
out.push_back(res.back());
res.pop_back();
}
res.push_back(tbl[i]);
}
return res;
}
long long minimum_walk(vector<int> p, int s) {
int n=(int)p.size();
vector<pair<int,int>> coord;
vector<seg> over;
vector<seg> nonover;
long long ans=0;
for (int i=0;i<n;i++) {
if (visit[i]) continue;
visit[i]=true;
int mn=i;
int mx=i;
int cl=-1e9;
int cr=1e9;
if (i<=s) cl=max(cl,i);
if (i>=s) cr=min(cr,i);
int cur=p[i];
ans+=abs(p[i]-i);
while (cur!=i) {
visit[cur]=true;
mx=max(mx,cur);
mn=min(mn,cur);
if (cur<=s) cl=max(cl,cur);
if (cur>=s) cr=min(cr,cur);
ans+=abs(p[cur]-cur);
cur=p[cur];
}
if (mn==mx) continue;
coord.push_back({mn,mx});
if (mn>=s||mx<=s) nonover.push_back(seg{cl,cr,mn,mx});
else over.push_back(seg{cl,cr,mn,mx});
}
coord.push_back({s,s});
auto re=uni(coord);
for (int i=1;i<(int)re.size();i++) {
ans+=(re[i].first-re[i-1].second)*2;
}
if (over.empty()) {
return ans;
}
//-------------------------------------------
vector<seg> byl=nonover;
vector<seg> byr=nonover;
sort(byr.begin(),byr.end(),[](const seg &a, const seg &b) {
return a.cr<b.cr;
});
while (!byr.empty()&&byr.back().cr==1e9) byr.pop_back();
sort(byl.begin(),byl.end(),[](const seg &a, const seg &b) {
return a.cl>b.cl;
});
while (!byl.empty()&&byl.back().cl==-1e9) byl.pop_back();
//-----------------------
while (!over.empty()) {
vector<seg> tmp;
auto res=rem(over,tmp);
over=tmp;
int tgtl=-1e9;
int tgtr=1e9;
for (auto &i:res) {
tgtl=max(tgtl,i.cl);
tgtr=min(tgtr,i.cr);
}
int stl=s;
int str=s;
for (auto &i:over) {
stl=min(stl,i.pl);
str=max(str,i.pr);
}
int curr=str;
long long addr=0;
for (auto &i:byr) {
if (curr>=tgtr) break;
if (i.cr>=tgtr) break;
if (i.cr>curr) {
addr+=(i.cr-curr)*2ll;
}
curr=max(curr,i.pr);
}
if (tgtr>curr) addr+=(tgtr-curr)*2ll;
int curl=stl;
long long addl=0;
for (auto &i:byl) {
if (curl<=tgtl) break;
if (i.cl<=tgtl) break;
if (i.cl<curl) {
addl+=(curl-i.cl)*2ll;
}
curl=min(curl,i.pl);
}
if (tgtl<curl) addl+=(curl-tgtl)*2ll;
ans+=min(addr,addl);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
379 ms |
9340 KB |
Output is correct |
31 |
Correct |
364 ms |
9468 KB |
Output is correct |
32 |
Correct |
157 ms |
9468 KB |
Output is correct |
33 |
Correct |
212 ms |
17500 KB |
Output is correct |
34 |
Correct |
226 ms |
17500 KB |
Output is correct |
35 |
Correct |
237 ms |
17936 KB |
Output is correct |
36 |
Correct |
205 ms |
17936 KB |
Output is correct |
37 |
Correct |
195 ms |
17936 KB |
Output is correct |
38 |
Correct |
172 ms |
17936 KB |
Output is correct |
39 |
Correct |
181 ms |
17936 KB |
Output is correct |
40 |
Correct |
165 ms |
17936 KB |
Output is correct |
41 |
Correct |
286 ms |
17936 KB |
Output is correct |
42 |
Correct |
232 ms |
17936 KB |
Output is correct |
43 |
Correct |
178 ms |
17936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
17936 KB |
Output is correct |
2 |
Correct |
3 ms |
17936 KB |
Output is correct |
3 |
Correct |
2 ms |
17936 KB |
Output is correct |
4 |
Correct |
2 ms |
17936 KB |
Output is correct |
5 |
Correct |
3 ms |
17936 KB |
Output is correct |
6 |
Correct |
2 ms |
17936 KB |
Output is correct |
7 |
Correct |
2 ms |
17936 KB |
Output is correct |
8 |
Correct |
3 ms |
17936 KB |
Output is correct |
9 |
Correct |
3 ms |
17936 KB |
Output is correct |
10 |
Correct |
4 ms |
17936 KB |
Output is correct |
11 |
Correct |
6 ms |
17936 KB |
Output is correct |
12 |
Correct |
5 ms |
17936 KB |
Output is correct |
13 |
Correct |
3 ms |
17936 KB |
Output is correct |
14 |
Correct |
2 ms |
17936 KB |
Output is correct |
15 |
Correct |
3 ms |
17936 KB |
Output is correct |
16 |
Correct |
3 ms |
17936 KB |
Output is correct |
17 |
Correct |
4 ms |
17936 KB |
Output is correct |
18 |
Correct |
3 ms |
17936 KB |
Output is correct |
19 |
Correct |
3 ms |
17936 KB |
Output is correct |
20 |
Correct |
3 ms |
17936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
379 ms |
9340 KB |
Output is correct |
31 |
Correct |
364 ms |
9468 KB |
Output is correct |
32 |
Correct |
157 ms |
9468 KB |
Output is correct |
33 |
Correct |
212 ms |
17500 KB |
Output is correct |
34 |
Correct |
226 ms |
17500 KB |
Output is correct |
35 |
Correct |
237 ms |
17936 KB |
Output is correct |
36 |
Correct |
205 ms |
17936 KB |
Output is correct |
37 |
Correct |
195 ms |
17936 KB |
Output is correct |
38 |
Correct |
172 ms |
17936 KB |
Output is correct |
39 |
Correct |
181 ms |
17936 KB |
Output is correct |
40 |
Correct |
165 ms |
17936 KB |
Output is correct |
41 |
Correct |
286 ms |
17936 KB |
Output is correct |
42 |
Correct |
232 ms |
17936 KB |
Output is correct |
43 |
Correct |
178 ms |
17936 KB |
Output is correct |
44 |
Correct |
2 ms |
17936 KB |
Output is correct |
45 |
Correct |
3 ms |
17936 KB |
Output is correct |
46 |
Correct |
2 ms |
17936 KB |
Output is correct |
47 |
Correct |
2 ms |
17936 KB |
Output is correct |
48 |
Correct |
3 ms |
17936 KB |
Output is correct |
49 |
Correct |
2 ms |
17936 KB |
Output is correct |
50 |
Correct |
2 ms |
17936 KB |
Output is correct |
51 |
Correct |
3 ms |
17936 KB |
Output is correct |
52 |
Correct |
3 ms |
17936 KB |
Output is correct |
53 |
Correct |
4 ms |
17936 KB |
Output is correct |
54 |
Correct |
6 ms |
17936 KB |
Output is correct |
55 |
Correct |
5 ms |
17936 KB |
Output is correct |
56 |
Correct |
3 ms |
17936 KB |
Output is correct |
57 |
Correct |
2 ms |
17936 KB |
Output is correct |
58 |
Correct |
3 ms |
17936 KB |
Output is correct |
59 |
Correct |
3 ms |
17936 KB |
Output is correct |
60 |
Correct |
4 ms |
17936 KB |
Output is correct |
61 |
Correct |
3 ms |
17936 KB |
Output is correct |
62 |
Correct |
3 ms |
17936 KB |
Output is correct |
63 |
Correct |
3 ms |
17936 KB |
Output is correct |
64 |
Correct |
209 ms |
22400 KB |
Output is correct |
65 |
Correct |
227 ms |
28608 KB |
Output is correct |
66 |
Correct |
195 ms |
31448 KB |
Output is correct |
67 |
Incorrect |
214 ms |
37492 KB |
3rd lines differ - on the 1st token, expected: '2408891936', found: '2408891938' |
68 |
Halted |
0 ms |
0 KB |
- |