#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++){
ans += abs(i - p[i]);
//cout << i << " " << p[i] << endl;
}
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;
cu = p[cu];
}
pi.pb({cl, cr});
}
}
for(int i=0; i<n; i++) nr[i] = nl[i] = -1;
//cout << ans << endl;
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=s; i<=lar; i++){
if(i > cr){
ans += 2;
cr++;
}
if(nr[i] != -1)
cr = max(cr, nr[i]);
}
for(int i=s; 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:48: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 |
3 ms |
384 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 |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 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 |
3 ms |
384 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 |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 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 |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 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 |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 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 |
3 ms |
384 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 |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 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 |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 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 |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
276 ms |
24796 KB |
Output is correct |
31 |
Correct |
250 ms |
24824 KB |
Output is correct |
32 |
Correct |
210 ms |
40660 KB |
Output is correct |
33 |
Correct |
191 ms |
35800 KB |
Output is correct |
34 |
Correct |
213 ms |
35768 KB |
Output is correct |
35 |
Correct |
180 ms |
33624 KB |
Output is correct |
36 |
Correct |
179 ms |
28868 KB |
Output is correct |
37 |
Correct |
159 ms |
25452 KB |
Output is correct |
38 |
Correct |
173 ms |
24952 KB |
Output is correct |
39 |
Correct |
170 ms |
25020 KB |
Output is correct |
40 |
Correct |
178 ms |
24796 KB |
Output is correct |
41 |
Correct |
220 ms |
24892 KB |
Output is correct |
42 |
Correct |
218 ms |
24828 KB |
Output is correct |
43 |
Correct |
190 ms |
36544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 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 |
3 ms |
384 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 |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 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 |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 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 |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
276 ms |
24796 KB |
Output is correct |
31 |
Correct |
250 ms |
24824 KB |
Output is correct |
32 |
Correct |
210 ms |
40660 KB |
Output is correct |
33 |
Correct |
191 ms |
35800 KB |
Output is correct |
34 |
Correct |
213 ms |
35768 KB |
Output is correct |
35 |
Correct |
180 ms |
33624 KB |
Output is correct |
36 |
Correct |
179 ms |
28868 KB |
Output is correct |
37 |
Correct |
159 ms |
25452 KB |
Output is correct |
38 |
Correct |
173 ms |
24952 KB |
Output is correct |
39 |
Correct |
170 ms |
25020 KB |
Output is correct |
40 |
Correct |
178 ms |
24796 KB |
Output is correct |
41 |
Correct |
220 ms |
24892 KB |
Output is correct |
42 |
Correct |
218 ms |
24828 KB |
Output is correct |
43 |
Correct |
190 ms |
36544 KB |
Output is correct |
44 |
Incorrect |
3 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |