#include "books.h"
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <cstdio>
using namespace std;
bool visit[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;
}
vector<pair<int,int>> rem(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++) {
while (!re.empty()&&tbl[i].first<=re.back().first) {
re.pop_back();
}
re.push_back(tbl[i]);
}
return re;
}
long long minimum_walk(vector<int> p, int s) {
int n=(int)p.size();
vector<pair<int,int>> coord;
vector<pair<int,int>> nonover;
vector<pair<int,int>> over;
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 cur=p[i];
ans+=abs(p[i]-i);
while (cur!=i) {
visit[cur]=true;
mx=max(mx,cur);
mn=min(mn,cur);
ans+=abs(p[cur]-cur);
cur=p[cur];
}
if (mn==mx) continue;
coord.push_back({mn,mx});
if (mn<=s&&s<=mx) over.push_back({mn,mx});
else nonover.push_back({mn,mx});
if (mn!=mx) coord.push_back({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;
}
auto over2=rem(over);
int ll=(over2.empty()?s:over2.back().first);
int rr=(over2.empty()?s:over2.front().second);
nonover.push_back({ll,ll});
nonover.push_back({rr,rr});
nonover.push_back({s,s});
auto nonover2=uni(nonover);
long long addr=0;
bool stflag=false;
for (int i=0;i<(int)nonover2.size();i++) {
if (nonover2[i].first<=s&&s<=nonover2[i].second) {
stflag=true;
if (nonover2[i].first<=rr&&rr<=nonover2[i].second) break;
continue;
}
if (!stflag) continue;
addr+=(nonover2[i].first-nonover2[i-1].second)*2;
if (nonover2[i].first<=rr&&rr<=nonover2[i].second) break;
}
stflag=false;
long long addl=0;
for (int i=(int)nonover2.size()-1;i>=0;i--) {
if (nonover2[i].first<=s&&s<=nonover2[i].second) {
stflag=true;
if (nonover2[i].first<=ll&&ll<=nonover2[i].second) break;
continue;
}
if (!stflag) continue;
addl+=(nonover2[i+1].first-nonover2[i].second)*2;
if (nonover2[i].first<=ll&&ll<=nonover2[i].second) break;
}
ans+=min(addl,addr);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
392 KB |
Output is correct |
4 |
Correct |
2 ms |
392 KB |
Output is correct |
5 |
Correct |
3 ms |
472 KB |
Output is correct |
6 |
Correct |
3 ms |
472 KB |
Output is correct |
7 |
Correct |
3 ms |
472 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
3 ms |
508 KB |
Output is correct |
10 |
Correct |
2 ms |
532 KB |
Output is correct |
11 |
Correct |
2 ms |
532 KB |
Output is correct |
12 |
Correct |
2 ms |
532 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
392 KB |
Output is correct |
4 |
Correct |
2 ms |
392 KB |
Output is correct |
5 |
Correct |
3 ms |
472 KB |
Output is correct |
6 |
Correct |
3 ms |
472 KB |
Output is correct |
7 |
Correct |
3 ms |
472 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
3 ms |
508 KB |
Output is correct |
10 |
Correct |
2 ms |
532 KB |
Output is correct |
11 |
Correct |
2 ms |
532 KB |
Output is correct |
12 |
Correct |
2 ms |
532 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 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 |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
4 ms |
660 KB |
Output is correct |
23 |
Correct |
4 ms |
792 KB |
Output is correct |
24 |
Correct |
3 ms |
792 KB |
Output is correct |
25 |
Correct |
3 ms |
792 KB |
Output is correct |
26 |
Correct |
2 ms |
792 KB |
Output is correct |
27 |
Correct |
2 ms |
792 KB |
Output is correct |
28 |
Correct |
4 ms |
792 KB |
Output is correct |
29 |
Correct |
3 ms |
792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
392 KB |
Output is correct |
4 |
Correct |
2 ms |
392 KB |
Output is correct |
5 |
Correct |
3 ms |
472 KB |
Output is correct |
6 |
Correct |
3 ms |
472 KB |
Output is correct |
7 |
Correct |
3 ms |
472 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
3 ms |
508 KB |
Output is correct |
10 |
Correct |
2 ms |
532 KB |
Output is correct |
11 |
Correct |
2 ms |
532 KB |
Output is correct |
12 |
Correct |
2 ms |
532 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 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 |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
4 ms |
660 KB |
Output is correct |
23 |
Correct |
4 ms |
792 KB |
Output is correct |
24 |
Correct |
3 ms |
792 KB |
Output is correct |
25 |
Correct |
3 ms |
792 KB |
Output is correct |
26 |
Correct |
2 ms |
792 KB |
Output is correct |
27 |
Correct |
2 ms |
792 KB |
Output is correct |
28 |
Correct |
4 ms |
792 KB |
Output is correct |
29 |
Correct |
3 ms |
792 KB |
Output is correct |
30 |
Correct |
279 ms |
14144 KB |
Output is correct |
31 |
Correct |
281 ms |
20844 KB |
Output is correct |
32 |
Correct |
148 ms |
27544 KB |
Output is correct |
33 |
Correct |
304 ms |
43236 KB |
Output is correct |
34 |
Correct |
261 ms |
43260 KB |
Output is correct |
35 |
Correct |
344 ms |
43512 KB |
Output is correct |
36 |
Correct |
285 ms |
43512 KB |
Output is correct |
37 |
Correct |
189 ms |
43512 KB |
Output is correct |
38 |
Correct |
176 ms |
43512 KB |
Output is correct |
39 |
Correct |
162 ms |
43512 KB |
Output is correct |
40 |
Correct |
194 ms |
43512 KB |
Output is correct |
41 |
Correct |
200 ms |
43512 KB |
Output is correct |
42 |
Correct |
180 ms |
43512 KB |
Output is correct |
43 |
Correct |
221 ms |
43512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
43512 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3312' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
392 KB |
Output is correct |
4 |
Correct |
2 ms |
392 KB |
Output is correct |
5 |
Correct |
3 ms |
472 KB |
Output is correct |
6 |
Correct |
3 ms |
472 KB |
Output is correct |
7 |
Correct |
3 ms |
472 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
3 ms |
508 KB |
Output is correct |
10 |
Correct |
2 ms |
532 KB |
Output is correct |
11 |
Correct |
2 ms |
532 KB |
Output is correct |
12 |
Correct |
2 ms |
532 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 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 |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
4 ms |
660 KB |
Output is correct |
23 |
Correct |
4 ms |
792 KB |
Output is correct |
24 |
Correct |
3 ms |
792 KB |
Output is correct |
25 |
Correct |
3 ms |
792 KB |
Output is correct |
26 |
Correct |
2 ms |
792 KB |
Output is correct |
27 |
Correct |
2 ms |
792 KB |
Output is correct |
28 |
Correct |
4 ms |
792 KB |
Output is correct |
29 |
Correct |
3 ms |
792 KB |
Output is correct |
30 |
Correct |
279 ms |
14144 KB |
Output is correct |
31 |
Correct |
281 ms |
20844 KB |
Output is correct |
32 |
Correct |
148 ms |
27544 KB |
Output is correct |
33 |
Correct |
304 ms |
43236 KB |
Output is correct |
34 |
Correct |
261 ms |
43260 KB |
Output is correct |
35 |
Correct |
344 ms |
43512 KB |
Output is correct |
36 |
Correct |
285 ms |
43512 KB |
Output is correct |
37 |
Correct |
189 ms |
43512 KB |
Output is correct |
38 |
Correct |
176 ms |
43512 KB |
Output is correct |
39 |
Correct |
162 ms |
43512 KB |
Output is correct |
40 |
Correct |
194 ms |
43512 KB |
Output is correct |
41 |
Correct |
200 ms |
43512 KB |
Output is correct |
42 |
Correct |
180 ms |
43512 KB |
Output is correct |
43 |
Correct |
221 ms |
43512 KB |
Output is correct |
44 |
Incorrect |
2 ms |
43512 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3312' |
45 |
Halted |
0 ms |
0 KB |
- |