#include "books.h"
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll MAXN = 1e6 + 10;
vector<pii> pi;
bool vis[MAXN];
ll n;
ll ans;
bool done[MAXN];
ll nr[MAXN], nl[MAXN];
long long minimum_walk(std::vector<int> p, int s) {
n = p.size();
for(int i=0; i<n; i++){
if(!vis[i]){
ll cl = i, cr = i;
ll cu = i;
while(!vis[cu]){
cl = min(cu, cl);
cr = max(cu, cr);
vis[cu] = 1;
ans += abs(cu - p[cu]);
cu = p[cu];
}
pi.pb({cl, cr});
}
}
for(int i=0; i<n; i++) nr[i] = nl[i] = -1;
ll lar = s, fil = s;
ll cl = s, cr = s;
for(int i=0; i<pi.size(); i++){
if(pi[i].x <= s && s <= pi[i].y){
cl = min(cl, pi[i].x);
cr = max(cr, pi[i].y);
}
nr[pi[i].x] = pi[i].y;
nl[pi[i].y] = pi[i].x;
if(pi[i].x != pi[i].y){
lar = max(lar, pi[i].y);
fil = min(fil, pi[i].x);
}
}
//cout << fil << " " << lar << endl;
for(int i=cl; i<=lar; i++){
if(i > cr){
ans += 2;
cr++;
}
if(nr[i] != -1)
cr = max(cr, nr[i]);
}
for(int i=cr; i>=fil; i--){
if(i < cl){
ans += 2;
cl--;
}
if(nl[i] != -1)
cl = min(cl, nl[i]);
}
return ans;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:42:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<pi.size(); i++){
~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
296 ms |
24800 KB |
Output is correct |
31 |
Correct |
271 ms |
24768 KB |
Output is correct |
32 |
Correct |
239 ms |
40744 KB |
Output is correct |
33 |
Correct |
194 ms |
35792 KB |
Output is correct |
34 |
Correct |
199 ms |
35792 KB |
Output is correct |
35 |
Correct |
193 ms |
33616 KB |
Output is correct |
36 |
Correct |
155 ms |
28896 KB |
Output is correct |
37 |
Correct |
160 ms |
25452 KB |
Output is correct |
38 |
Correct |
137 ms |
25080 KB |
Output is correct |
39 |
Correct |
150 ms |
24952 KB |
Output is correct |
40 |
Correct |
148 ms |
24796 KB |
Output is correct |
41 |
Correct |
170 ms |
24764 KB |
Output is correct |
42 |
Correct |
249 ms |
24820 KB |
Output is correct |
43 |
Correct |
178 ms |
36512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
296 ms |
24800 KB |
Output is correct |
31 |
Correct |
271 ms |
24768 KB |
Output is correct |
32 |
Correct |
239 ms |
40744 KB |
Output is correct |
33 |
Correct |
194 ms |
35792 KB |
Output is correct |
34 |
Correct |
199 ms |
35792 KB |
Output is correct |
35 |
Correct |
193 ms |
33616 KB |
Output is correct |
36 |
Correct |
155 ms |
28896 KB |
Output is correct |
37 |
Correct |
160 ms |
25452 KB |
Output is correct |
38 |
Correct |
137 ms |
25080 KB |
Output is correct |
39 |
Correct |
150 ms |
24952 KB |
Output is correct |
40 |
Correct |
148 ms |
24796 KB |
Output is correct |
41 |
Correct |
170 ms |
24764 KB |
Output is correct |
42 |
Correct |
249 ms |
24820 KB |
Output is correct |
43 |
Correct |
178 ms |
36512 KB |
Output is correct |
44 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |