#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.pr!=b.pr) return a.pr<b.pr;
return a.pl>b.pl;
});
vector<seg> res;
for (int i=0;i<(int)tbl.size();i++) {
while (!res.empty()&&tbl[i].pl<=res.back().pl) {
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>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<curl) {
addl+=(curl-i.cl)*2ll;
}
curl=min(curl,i.pl);
}
if (tgtl<curl) addl+=(curl-tgtl)*2ll;
ans+=min(addr,addl);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
608 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
608 KB |
Output is correct |
6 |
Correct |
2 ms |
608 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
3 ms |
608 KB |
Output is correct |
9 |
Correct |
2 ms |
608 KB |
Output is correct |
10 |
Correct |
2 ms |
608 KB |
Output is correct |
11 |
Correct |
3 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
4 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
608 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
608 KB |
Output is correct |
6 |
Correct |
2 ms |
608 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
3 ms |
608 KB |
Output is correct |
9 |
Correct |
2 ms |
608 KB |
Output is correct |
10 |
Correct |
2 ms |
608 KB |
Output is correct |
11 |
Correct |
3 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
4 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 |
2 ms |
620 KB |
Output is correct |
22 |
Correct |
3 ms |
660 KB |
Output is correct |
23 |
Correct |
3 ms |
660 KB |
Output is correct |
24 |
Correct |
2 ms |
660 KB |
Output is correct |
25 |
Correct |
3 ms |
668 KB |
Output is correct |
26 |
Correct |
2 ms |
668 KB |
Output is correct |
27 |
Correct |
3 ms |
668 KB |
Output is correct |
28 |
Correct |
2 ms |
668 KB |
Output is correct |
29 |
Correct |
2 ms |
680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
608 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
608 KB |
Output is correct |
6 |
Correct |
2 ms |
608 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
3 ms |
608 KB |
Output is correct |
9 |
Correct |
2 ms |
608 KB |
Output is correct |
10 |
Correct |
2 ms |
608 KB |
Output is correct |
11 |
Correct |
3 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
4 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 |
2 ms |
620 KB |
Output is correct |
22 |
Correct |
3 ms |
660 KB |
Output is correct |
23 |
Correct |
3 ms |
660 KB |
Output is correct |
24 |
Correct |
2 ms |
660 KB |
Output is correct |
25 |
Correct |
3 ms |
668 KB |
Output is correct |
26 |
Correct |
2 ms |
668 KB |
Output is correct |
27 |
Correct |
3 ms |
668 KB |
Output is correct |
28 |
Correct |
2 ms |
668 KB |
Output is correct |
29 |
Correct |
2 ms |
680 KB |
Output is correct |
30 |
Correct |
330 ms |
12608 KB |
Output is correct |
31 |
Correct |
286 ms |
12632 KB |
Output is correct |
32 |
Correct |
164 ms |
12632 KB |
Output is correct |
33 |
Correct |
234 ms |
21188 KB |
Output is correct |
34 |
Correct |
235 ms |
24292 KB |
Output is correct |
35 |
Correct |
248 ms |
24928 KB |
Output is correct |
36 |
Correct |
307 ms |
24928 KB |
Output is correct |
37 |
Correct |
185 ms |
24928 KB |
Output is correct |
38 |
Correct |
171 ms |
24928 KB |
Output is correct |
39 |
Correct |
180 ms |
24928 KB |
Output is correct |
40 |
Correct |
203 ms |
24928 KB |
Output is correct |
41 |
Correct |
247 ms |
24928 KB |
Output is correct |
42 |
Correct |
211 ms |
24928 KB |
Output is correct |
43 |
Correct |
221 ms |
24928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
24928 KB |
Output is correct |
2 |
Correct |
3 ms |
24928 KB |
Output is correct |
3 |
Correct |
2 ms |
24928 KB |
Output is correct |
4 |
Correct |
3 ms |
24928 KB |
Output is correct |
5 |
Correct |
3 ms |
24928 KB |
Output is correct |
6 |
Incorrect |
3 ms |
24928 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22240' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
608 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
608 KB |
Output is correct |
6 |
Correct |
2 ms |
608 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
3 ms |
608 KB |
Output is correct |
9 |
Correct |
2 ms |
608 KB |
Output is correct |
10 |
Correct |
2 ms |
608 KB |
Output is correct |
11 |
Correct |
3 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
4 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 |
2 ms |
620 KB |
Output is correct |
22 |
Correct |
3 ms |
660 KB |
Output is correct |
23 |
Correct |
3 ms |
660 KB |
Output is correct |
24 |
Correct |
2 ms |
660 KB |
Output is correct |
25 |
Correct |
3 ms |
668 KB |
Output is correct |
26 |
Correct |
2 ms |
668 KB |
Output is correct |
27 |
Correct |
3 ms |
668 KB |
Output is correct |
28 |
Correct |
2 ms |
668 KB |
Output is correct |
29 |
Correct |
2 ms |
680 KB |
Output is correct |
30 |
Correct |
330 ms |
12608 KB |
Output is correct |
31 |
Correct |
286 ms |
12632 KB |
Output is correct |
32 |
Correct |
164 ms |
12632 KB |
Output is correct |
33 |
Correct |
234 ms |
21188 KB |
Output is correct |
34 |
Correct |
235 ms |
24292 KB |
Output is correct |
35 |
Correct |
248 ms |
24928 KB |
Output is correct |
36 |
Correct |
307 ms |
24928 KB |
Output is correct |
37 |
Correct |
185 ms |
24928 KB |
Output is correct |
38 |
Correct |
171 ms |
24928 KB |
Output is correct |
39 |
Correct |
180 ms |
24928 KB |
Output is correct |
40 |
Correct |
203 ms |
24928 KB |
Output is correct |
41 |
Correct |
247 ms |
24928 KB |
Output is correct |
42 |
Correct |
211 ms |
24928 KB |
Output is correct |
43 |
Correct |
221 ms |
24928 KB |
Output is correct |
44 |
Correct |
2 ms |
24928 KB |
Output is correct |
45 |
Correct |
3 ms |
24928 KB |
Output is correct |
46 |
Correct |
2 ms |
24928 KB |
Output is correct |
47 |
Correct |
3 ms |
24928 KB |
Output is correct |
48 |
Correct |
3 ms |
24928 KB |
Output is correct |
49 |
Incorrect |
3 ms |
24928 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22240' |
50 |
Halted |
0 ms |
0 KB |
- |