// moreflags=grader.cpp
// 11
// late
// ...
//
#include "books.h"
#include<algorithm>
#include<numeric>
#if not LOCAL
#define NDEBUG
#endif
#include<cassert>
#include<climits>
long long minimum_walk(std::vector<int> p, int s) {
assert(s>=0); assert(s<(int)p.size());
int64_t result{};
for(int index=0; index<(int)p.size(); ++index){
if(index<p[index]){
result+=p[index]-index;
}
}
struct Dsu{
std::vector<int> data;
void reset(int number){data.assign(number, -1);}
int root(int node){return data[node]>=0 ? data[node]=root(data[node]): node;}
bool join(int first, int sec){
first=root(first); sec=root(sec);
if(first==sec) return false;
data[first]=sec;
return true;
}
};
Dsu dsu; dsu.reset((int)p.size());
/*
for(int first=0; first<(int)p.size(); ++first) if(p[first]!=first)
for(int sec=0; sec<first; ++sec) if(p[sec]!=sec){
auto const [p1, p2]=std::minmax({
std::minmax({first, p[first]}),
std::minmax({sec, p[sec]})
});
auto const [a, b]=p1; auto const [c, d]=p2;
if(c<=b and b<=d){// partially intersect
dsu.join(first, sec);
}
}
*/
struct Event{int id, pos;
bool operator<(Event other) const{return pos<other.pos;}
};
std::vector<Event> events; events.reserve(p.size()*2);
for(int index=0; index<(int)p.size(); ++index) if(index!=p[index]){
auto const [a, b]=std::minmax({index, p[index]});
dsu.join(a, b);
events.push_back({index, a*2});
events.push_back({index, b*2+1}); // sort open before close in pos
}
std::sort(begin(events), end(events));
std::vector<int> depth(p.size(), -1); // in stack (-2: already popped)
struct Item{
int firstConnect; // stack[firstConnect]..=current item should be connected (or INT_MAX)
int id;
};
std::vector<Item> stack;
for(auto [id, _pos]: events){
if(depth[id]<0){
assert(depth[id]==-1);
depth[id]=(int)stack.size();
stack.push_back({INT_MAX, id});
}else{
auto const curDepth=depth[id];
assert(stack[curDepth].id==id);
if(stack[curDepth].firstConnect<curDepth){
stack[curDepth-1].firstConnect=std::min(stack[curDepth-1].firstConnect,
stack[curDepth].firstConnect);
if(stack[curDepth-1].id>=0)
dsu.join(stack[curDepth-1].id, id);
}
stack[curDepth].id=-1;
bool const removeBack=curDepth==(int)stack.size()-1;
depth[id]=-2;
if(not removeBack){
dsu.join(id, stack.back().id);
}
int minFirstConnect=curDepth;
while(not stack.empty() and stack.back().id<0){
assert(removeBack);
assert(stack.back().id==-1);
minFirstConnect=std::min(minFirstConnect, stack.back().firstConnect);
stack.pop_back();
}
if(minFirstConnect<(int)stack.size()){
stack.back().firstConnect=std::min(stack.back().firstConnect,
minFirstConnect);
dsu.join(id, stack.back().id);
}
}
}
assert(([&]{
for(int first=0; first<(int)p.size(); ++first) if(p[first]!=first)
for(int sec=0; sec<first; ++sec) if(p[sec]!=sec){
auto const [p1, p2]=std::minmax({
std::minmax({first, p[first]}),
std::minmax({sec, p[sec]})
});
auto const [a, b]=p1; auto const [c, d]=p2;
if(c<=b and b<=d){// partially intersect
assert(dsu.root(first)==dsu.root(sec));
//dsu.join(first, sec);
}
}
return true;
}()));
std::vector<std::vector<int>> components(p.size());
for(int index=0; index<(int)p.size(); ++index)
if(p[index]!=index)
components[dsu.root(index)].push_back(index);
components.erase(std::remove_if(begin(components), end(components), [&](auto const& it){
return it.empty();}), components.end());
std::vector<int> left(p.size(), INT_MAX), right(p.size(), -1);
for(auto const& it: components)
for(auto item: it)
left[item]=it[0], right[item]=it.back();
int left_=std::min(s, left[s]), right_=std::max(s, right[s]);
while(true){
int left1=left_, cost1=0;
while(left1>=0 and right[left1]<=right_){
if(left[left1]<=left1)
left1=left[left1];
assert(left[left1]>=left1);
++cost1;
--left1;
}
int right2=right_, cost2=0;
while(right2<(int)p.size() and left[right2]>=left_){
if(right[right2]>=right2)
right2=right[right2];
assert(right[right2]<=right2);
++cost2;
++right2;
}
assert((left1>=0)==(right2<(int)p.size()));
if(left1<0){
break;
}
if(cost1<cost2){
result+=cost1;
}else{
result+=cost2;
}
assert(left[left1]==left[right2]);
assert(right[left1]==right[right2]);
left_=left[left1]; right_=right[right2];
assert(left[left_]>=left_);
assert(right[right_]<=right_);
}
while(true){
assert(left[left_]>=left_);
int left1=left_;
while(left1>=0 and left[left1]>=left1) --left1;
if(left1<0) break;
assert(right[left1]<left_);
result+=left_-left1; left_=left[left1];
}
while(true){
assert(right[right_]<=right_);
int right2=right_;
while(right2<(int)p.size() and right[right2]<=right2) ++right2;
if(right2==(int)p.size()) break;
assert(left[right2]>right_);
result+=right2-right_; right_=right[right2];
}
return result*2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
416 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
416 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
732 ms |
77900 KB |
Output is correct |
31 |
Correct |
701 ms |
81620 KB |
Output is correct |
32 |
Correct |
191 ms |
54008 KB |
Output is correct |
33 |
Correct |
295 ms |
67832 KB |
Output is correct |
34 |
Correct |
303 ms |
67904 KB |
Output is correct |
35 |
Correct |
334 ms |
71288 KB |
Output is correct |
36 |
Correct |
373 ms |
74616 KB |
Output is correct |
37 |
Correct |
393 ms |
75304 KB |
Output is correct |
38 |
Correct |
427 ms |
75256 KB |
Output is correct |
39 |
Correct |
431 ms |
74872 KB |
Output is correct |
40 |
Correct |
433 ms |
74736 KB |
Output is correct |
41 |
Correct |
572 ms |
78692 KB |
Output is correct |
42 |
Correct |
498 ms |
78048 KB |
Output is correct |
43 |
Correct |
296 ms |
69752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
416 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
732 ms |
77900 KB |
Output is correct |
31 |
Correct |
701 ms |
81620 KB |
Output is correct |
32 |
Correct |
191 ms |
54008 KB |
Output is correct |
33 |
Correct |
295 ms |
67832 KB |
Output is correct |
34 |
Correct |
303 ms |
67904 KB |
Output is correct |
35 |
Correct |
334 ms |
71288 KB |
Output is correct |
36 |
Correct |
373 ms |
74616 KB |
Output is correct |
37 |
Correct |
393 ms |
75304 KB |
Output is correct |
38 |
Correct |
427 ms |
75256 KB |
Output is correct |
39 |
Correct |
431 ms |
74872 KB |
Output is correct |
40 |
Correct |
433 ms |
74736 KB |
Output is correct |
41 |
Correct |
572 ms |
78692 KB |
Output is correct |
42 |
Correct |
498 ms |
78048 KB |
Output is correct |
43 |
Correct |
296 ms |
69752 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
1 ms |
384 KB |
Output is correct |
54 |
Correct |
1 ms |
384 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
1 ms |
384 KB |
Output is correct |
57 |
Correct |
1 ms |
360 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
263 ms |
63388 KB |
Output is correct |
65 |
Correct |
270 ms |
61688 KB |
Output is correct |
66 |
Correct |
389 ms |
74656 KB |
Output is correct |
67 |
Correct |
391 ms |
74412 KB |
Output is correct |
68 |
Correct |
35 ms |
7416 KB |
Output is correct |
69 |
Correct |
37 ms |
7544 KB |
Output is correct |
70 |
Correct |
38 ms |
7168 KB |
Output is correct |
71 |
Correct |
35 ms |
6888 KB |
Output is correct |
72 |
Correct |
29 ms |
6272 KB |
Output is correct |
73 |
Correct |
44 ms |
7924 KB |
Output is correct |
74 |
Correct |
285 ms |
68012 KB |
Output is correct |
75 |
Correct |
341 ms |
67832 KB |
Output is correct |
76 |
Correct |
299 ms |
70008 KB |
Output is correct |
77 |
Correct |
302 ms |
69792 KB |
Output is correct |
78 |
Correct |
376 ms |
77076 KB |
Output is correct |
79 |
Correct |
338 ms |
77048 KB |
Output is correct |
80 |
Correct |
195 ms |
54012 KB |
Output is correct |
81 |
Correct |
627 ms |
90560 KB |
Output is correct |
82 |
Correct |
653 ms |
90524 KB |
Output is correct |
83 |
Correct |
376 ms |
75808 KB |
Output is correct |
84 |
Correct |
387 ms |
76056 KB |
Output is correct |
85 |
Correct |
378 ms |
74144 KB |
Output is correct |
86 |
Correct |
405 ms |
74788 KB |
Output is correct |
87 |
Correct |
325 ms |
66016 KB |
Output is correct |
88 |
Correct |
313 ms |
73936 KB |
Output is correct |
89 |
Correct |
360 ms |
76140 KB |
Output is correct |
90 |
Correct |
400 ms |
75768 KB |
Output is correct |
91 |
Correct |
396 ms |
73720 KB |
Output is correct |
92 |
Correct |
432 ms |
74336 KB |
Output is correct |