#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef pair<int, int> pii;
typedef long long lld;
typedef long double ld;
template<typename Iter>
ostream& outIt(ostream& out, Iter b, Iter e){
for(Iter i = b; i != e; ++i)
out << (i == b?"":" ") << *i;
return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> p){
return out << '(' << p.ff << ", " << p.ss << ')';
}
template<typename T>
ostream& operator<<(ostream& out, vector<T> v){
return outIt(out << '[', all(v)) << ']';
}
vector<pii> cs;
vector<int> poss, dist, gone;
set<int> not_yet;
long long minimum_walk(vector<int> p, int s) {
lld ans = 0;
int pmin = INT_MAX, pmax = INT_MIN;
for(int i = 0; i < p.size(); i++)if(p[i] != i)ans += abs(p[i]-i), cs.pb({min(i, p[i]), max(i, p[i])}), pmin = min(pmin, i), pmax = max(pmax, i);
if(ans == 0)return 0;
sort(all(cs));
int r = cs.front().ff;
gone.resize(p.size()), poss.resize(p.size()), dist.resize(p.size());
poss[r] = 1;
bool fl = true;
for(int i = 0; i < cs.size(); i++){
if(cs[i].ff > s && s > r)fl = false;
if(cs[i].ff > r) poss[r] = poss[cs[i].ff] = 1;
ans += 2*max(0, cs[i].ff - r);
r = max(r, cs[i].ss);
}
poss[r] = 1;
//cout << poss << endl;
if(!fl)return ans;
for(int i = 0; i < p.size(); i++)not_yet.insert(i);
for(int d = 0, l = s, r = s; d < p.size(); d++, l--, r++){
while(not_yet.lower_bound(l) != not_yet.upper_bound(r)){
int ta = *not_yet.lower_bound(l);
not_yet.erase(ta);
//cout << ta << endl;
if(poss[ta])return ans + 2*d;
for(int x = p[ta]; not_yet.count(x); x = p[x]){
not_yet.erase(x), l = min(l, x), r = max(r, x);
if(poss[x])return ans + 2*d;
//cout << x << endl;
}
}
}
assert(false);
for(int i = 0; i < p.size(); i++)dist[i] = abs(i-s);
//cout << dist << gone << endl;
for(int i = 0; i < p.size(); i++){
pii mins = {INT_MAX, -1};
for(int j = 0; j < p.size(); j++)if(!gone[j])mins = min(mins, {dist[j], j});
gone[mins.ss] = true;
int ll = mins.ss, rr = p[mins.ss]; if(ll > rr)swap(ll, rr);
for(int j = 0; j < p.size(); j++){
dist[j] = min(dist[j], mins.ff + (ll <= j && j <= rr? 0:abs(mins.ss - j)));
}
}
//for(int i = 0; i < p.size(); i++)if(p[i] == i)dist[i] *= 0;
//cout << dist << endl;
//return ans + 2 * (*max_element(all(dist)));
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:35:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0; i < p.size(); i++)if(p[i] != i)ans += abs(p[i]-i), cs.pb({min(i, p[i]), max(i, p[i])}), pmin = min(pmin, i), pmax = max(pmax, i);
| ~~^~~~~~~~~~
books.cpp:42:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i = 0; i < cs.size(); i++){
| ~~^~~~~~~~~~~
books.cpp:51:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i = 0; i < p.size(); i++)not_yet.insert(i);
| ~~^~~~~~~~~~
books.cpp:52:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int d = 0, l = s, r = s; d < p.size(); d++, l--, r++){
| ~~^~~~~~~~~~
books.cpp:70:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 0; i < p.size(); i++)dist[i] = abs(i-s);
| ~~^~~~~~~~~~
books.cpp:72:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < p.size(); i++){
| ~~^~~~~~~~~~
books.cpp:74:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int j = 0; j < p.size(); j++)if(!gone[j])mins = min(mins, {dist[j], j});
| ~~^~~~~~~~~~
books.cpp:78:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int j = 0; j < p.size(); j++){
| ~~^~~~~~~~~~
# |
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 |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 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 |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
256 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 |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
256 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 |
518 ms |
74776 KB |
Output is correct |
31 |
Correct |
519 ms |
74708 KB |
Output is correct |
32 |
Correct |
126 ms |
8184 KB |
Output is correct |
33 |
Correct |
406 ms |
70932 KB |
Output is correct |
34 |
Correct |
393 ms |
70816 KB |
Output is correct |
35 |
Correct |
422 ms |
72024 KB |
Output is correct |
36 |
Correct |
435 ms |
73940 KB |
Output is correct |
37 |
Correct |
458 ms |
74708 KB |
Output is correct |
38 |
Correct |
467 ms |
74708 KB |
Output is correct |
39 |
Correct |
464 ms |
74712 KB |
Output is correct |
40 |
Correct |
492 ms |
74708 KB |
Output is correct |
41 |
Correct |
512 ms |
74836 KB |
Output is correct |
42 |
Correct |
504 ms |
74712 KB |
Output is correct |
43 |
Correct |
425 ms |
70880 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 |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 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 |
384 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 |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
256 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 |
518 ms |
74776 KB |
Output is correct |
31 |
Correct |
519 ms |
74708 KB |
Output is correct |
32 |
Correct |
126 ms |
8184 KB |
Output is correct |
33 |
Correct |
406 ms |
70932 KB |
Output is correct |
34 |
Correct |
393 ms |
70816 KB |
Output is correct |
35 |
Correct |
422 ms |
72024 KB |
Output is correct |
36 |
Correct |
435 ms |
73940 KB |
Output is correct |
37 |
Correct |
458 ms |
74708 KB |
Output is correct |
38 |
Correct |
467 ms |
74708 KB |
Output is correct |
39 |
Correct |
464 ms |
74712 KB |
Output is correct |
40 |
Correct |
492 ms |
74708 KB |
Output is correct |
41 |
Correct |
512 ms |
74836 KB |
Output is correct |
42 |
Correct |
504 ms |
74712 KB |
Output is correct |
43 |
Correct |
425 ms |
70880 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 |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
0 ms |
256 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 |
384 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 |
460 ms |
69804 KB |
Output is correct |
65 |
Correct |
503 ms |
73312 KB |
Output is correct |
66 |
Correct |
786 ms |
77912 KB |
Output is correct |
67 |
Correct |
790 ms |
77880 KB |
Output is correct |
68 |
Correct |
44 ms |
8172 KB |
Output is correct |
69 |
Correct |
61 ms |
8344 KB |
Output is correct |
70 |
Correct |
45 ms |
8172 KB |
Output is correct |
71 |
Correct |
43 ms |
7952 KB |
Output is correct |
72 |
Correct |
56 ms |
7796 KB |
Output is correct |
73 |
Correct |
66 ms |
8300 KB |
Output is correct |
74 |
Correct |
414 ms |
73828 KB |
Output is correct |
75 |
Correct |
164 ms |
27140 KB |
Output is correct |
76 |
Correct |
455 ms |
74124 KB |
Output is correct |
77 |
Correct |
409 ms |
73824 KB |
Output is correct |
78 |
Correct |
786 ms |
75912 KB |
Output is correct |
79 |
Correct |
745 ms |
75696 KB |
Output is correct |
80 |
Correct |
129 ms |
11256 KB |
Output is correct |
81 |
Correct |
823 ms |
76756 KB |
Output is correct |
82 |
Correct |
828 ms |
76756 KB |
Output is correct |
83 |
Correct |
819 ms |
75988 KB |
Output is correct |
84 |
Correct |
804 ms |
76116 KB |
Output is correct |
85 |
Correct |
780 ms |
76756 KB |
Output is correct |
86 |
Correct |
810 ms |
76820 KB |
Output is correct |
87 |
Correct |
834 ms |
71392 KB |
Output is correct |
88 |
Correct |
883 ms |
73584 KB |
Output is correct |
89 |
Correct |
877 ms |
75464 KB |
Output is correct |
90 |
Correct |
801 ms |
76756 KB |
Output is correct |
91 |
Correct |
835 ms |
76792 KB |
Output is correct |
92 |
Correct |
911 ms |
76884 KB |
Output is correct |