#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
bool visited[maxn];
int c[maxn];
vector<pair<int,int>> g[maxn];
ll dp[maxn];
long long MST(int src){
ll ret = 0;
memset(dp, 63, sizeof dp);
dp[src] = 0;
set<pair<ll,int>> S;
S.insert({dp[src],src});
while (!S.empty()){
int v = (*S.begin()).second;
S.erase(S.begin());
ret += dp[v];
for (auto [u,w] : g[v]){
if (dp[u] > w){
S.erase({dp[u],u});
dp[u] = w;
S.insert({dp[u],u});
}
}
}
return 2LL*ret;
}
long long minimum_walk(vector<int> p, int s){
int n = p.size();
set<int> S;
ll answer = 0;
for (int i = 0; i < n; i++)
answer += abs(p[i]-i);
int Max, Min, last;
Max = s, last = s+1;
for (int i = s; i < n; i++){
if (!visited[i] and p[i] != i){
int x = i;
c[i] = i+1;
if (i != s){
g[last].push_back({c[i],max(0,i-Max)});
g[c[i]].push_back({last,0});
}
while (!visited[x]){
visited[x] = 1;
c[x] = c[i];
if (x > Max){
Max = x;
last = c[x];
}
x = p[x];
}
}
}
Min = s, last = s+1;
memset(visited, 0, sizeof visited);
for (int i = s; i >= 0; i--){
if (!visited[i]){
int x = i;
if (c[i] == 0)
c[i] = i+1;
if (i != s){
g[last].push_back({c[i],max(0,Min-i)});
g[c[i]].push_back({last,0});
}
while (!visited[x]){
visited[x] = 1;
c[x] = c[i];
if (Min > x){
Min = x;
last = c[x];
}
x = p[x];
}
}
}
answer += MST(s+1);
return answer;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:37:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
37 | for (int i = 0; i < n; i++)
| ^~~
books.cpp:39:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
39 | int Max, Min, last;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
32640 KB |
Output is correct |
2 |
Correct |
22 ms |
32640 KB |
Output is correct |
3 |
Correct |
22 ms |
32640 KB |
Output is correct |
4 |
Correct |
22 ms |
32640 KB |
Output is correct |
5 |
Correct |
22 ms |
32640 KB |
Output is correct |
6 |
Correct |
21 ms |
32640 KB |
Output is correct |
7 |
Correct |
22 ms |
32640 KB |
Output is correct |
8 |
Correct |
21 ms |
32640 KB |
Output is correct |
9 |
Correct |
22 ms |
32640 KB |
Output is correct |
10 |
Correct |
22 ms |
32640 KB |
Output is correct |
11 |
Correct |
22 ms |
32640 KB |
Output is correct |
12 |
Correct |
22 ms |
32640 KB |
Output is correct |
13 |
Correct |
21 ms |
32640 KB |
Output is correct |
14 |
Correct |
22 ms |
32640 KB |
Output is correct |
15 |
Correct |
22 ms |
32640 KB |
Output is correct |
16 |
Correct |
21 ms |
32640 KB |
Output is correct |
17 |
Correct |
23 ms |
32640 KB |
Output is correct |
18 |
Correct |
22 ms |
32640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
32640 KB |
Output is correct |
2 |
Correct |
22 ms |
32640 KB |
Output is correct |
3 |
Correct |
22 ms |
32640 KB |
Output is correct |
4 |
Correct |
22 ms |
32640 KB |
Output is correct |
5 |
Correct |
22 ms |
32640 KB |
Output is correct |
6 |
Correct |
21 ms |
32640 KB |
Output is correct |
7 |
Correct |
22 ms |
32640 KB |
Output is correct |
8 |
Correct |
21 ms |
32640 KB |
Output is correct |
9 |
Correct |
22 ms |
32640 KB |
Output is correct |
10 |
Correct |
22 ms |
32640 KB |
Output is correct |
11 |
Correct |
22 ms |
32640 KB |
Output is correct |
12 |
Correct |
22 ms |
32640 KB |
Output is correct |
13 |
Correct |
21 ms |
32640 KB |
Output is correct |
14 |
Correct |
22 ms |
32640 KB |
Output is correct |
15 |
Correct |
22 ms |
32640 KB |
Output is correct |
16 |
Correct |
21 ms |
32640 KB |
Output is correct |
17 |
Correct |
23 ms |
32640 KB |
Output is correct |
18 |
Correct |
22 ms |
32640 KB |
Output is correct |
19 |
Correct |
21 ms |
32640 KB |
Output is correct |
20 |
Correct |
22 ms |
32632 KB |
Output is correct |
21 |
Correct |
22 ms |
32640 KB |
Output is correct |
22 |
Correct |
23 ms |
32760 KB |
Output is correct |
23 |
Correct |
22 ms |
32632 KB |
Output is correct |
24 |
Correct |
23 ms |
32640 KB |
Output is correct |
25 |
Correct |
22 ms |
32640 KB |
Output is correct |
26 |
Correct |
21 ms |
32640 KB |
Output is correct |
27 |
Correct |
22 ms |
32640 KB |
Output is correct |
28 |
Correct |
22 ms |
32640 KB |
Output is correct |
29 |
Correct |
23 ms |
32640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
32640 KB |
Output is correct |
2 |
Correct |
22 ms |
32640 KB |
Output is correct |
3 |
Correct |
22 ms |
32640 KB |
Output is correct |
4 |
Correct |
22 ms |
32640 KB |
Output is correct |
5 |
Correct |
22 ms |
32640 KB |
Output is correct |
6 |
Correct |
21 ms |
32640 KB |
Output is correct |
7 |
Correct |
22 ms |
32640 KB |
Output is correct |
8 |
Correct |
21 ms |
32640 KB |
Output is correct |
9 |
Correct |
22 ms |
32640 KB |
Output is correct |
10 |
Correct |
22 ms |
32640 KB |
Output is correct |
11 |
Correct |
22 ms |
32640 KB |
Output is correct |
12 |
Correct |
22 ms |
32640 KB |
Output is correct |
13 |
Correct |
21 ms |
32640 KB |
Output is correct |
14 |
Correct |
22 ms |
32640 KB |
Output is correct |
15 |
Correct |
22 ms |
32640 KB |
Output is correct |
16 |
Correct |
21 ms |
32640 KB |
Output is correct |
17 |
Correct |
23 ms |
32640 KB |
Output is correct |
18 |
Correct |
22 ms |
32640 KB |
Output is correct |
19 |
Correct |
21 ms |
32640 KB |
Output is correct |
20 |
Correct |
22 ms |
32632 KB |
Output is correct |
21 |
Correct |
22 ms |
32640 KB |
Output is correct |
22 |
Correct |
23 ms |
32760 KB |
Output is correct |
23 |
Correct |
22 ms |
32632 KB |
Output is correct |
24 |
Correct |
23 ms |
32640 KB |
Output is correct |
25 |
Correct |
22 ms |
32640 KB |
Output is correct |
26 |
Correct |
21 ms |
32640 KB |
Output is correct |
27 |
Correct |
22 ms |
32640 KB |
Output is correct |
28 |
Correct |
22 ms |
32640 KB |
Output is correct |
29 |
Correct |
23 ms |
32640 KB |
Output is correct |
30 |
Correct |
340 ms |
44428 KB |
Output is correct |
31 |
Correct |
305 ms |
44536 KB |
Output is correct |
32 |
Correct |
180 ms |
40440 KB |
Output is correct |
33 |
Correct |
226 ms |
50428 KB |
Output is correct |
34 |
Correct |
231 ms |
50552 KB |
Output is correct |
35 |
Correct |
234 ms |
51320 KB |
Output is correct |
36 |
Correct |
219 ms |
49784 KB |
Output is correct |
37 |
Correct |
198 ms |
45324 KB |
Output is correct |
38 |
Correct |
191 ms |
44620 KB |
Output is correct |
39 |
Correct |
194 ms |
44536 KB |
Output is correct |
40 |
Correct |
202 ms |
44408 KB |
Output is correct |
41 |
Correct |
216 ms |
44408 KB |
Output is correct |
42 |
Correct |
215 ms |
44664 KB |
Output is correct |
43 |
Correct |
323 ms |
69640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
32640 KB |
Output is correct |
2 |
Correct |
22 ms |
32640 KB |
Output is correct |
3 |
Correct |
22 ms |
32632 KB |
Output is correct |
4 |
Correct |
22 ms |
32684 KB |
Output is correct |
5 |
Correct |
22 ms |
32640 KB |
Output is correct |
6 |
Incorrect |
22 ms |
32640 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22144' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
32640 KB |
Output is correct |
2 |
Correct |
22 ms |
32640 KB |
Output is correct |
3 |
Correct |
22 ms |
32640 KB |
Output is correct |
4 |
Correct |
22 ms |
32640 KB |
Output is correct |
5 |
Correct |
22 ms |
32640 KB |
Output is correct |
6 |
Correct |
21 ms |
32640 KB |
Output is correct |
7 |
Correct |
22 ms |
32640 KB |
Output is correct |
8 |
Correct |
21 ms |
32640 KB |
Output is correct |
9 |
Correct |
22 ms |
32640 KB |
Output is correct |
10 |
Correct |
22 ms |
32640 KB |
Output is correct |
11 |
Correct |
22 ms |
32640 KB |
Output is correct |
12 |
Correct |
22 ms |
32640 KB |
Output is correct |
13 |
Correct |
21 ms |
32640 KB |
Output is correct |
14 |
Correct |
22 ms |
32640 KB |
Output is correct |
15 |
Correct |
22 ms |
32640 KB |
Output is correct |
16 |
Correct |
21 ms |
32640 KB |
Output is correct |
17 |
Correct |
23 ms |
32640 KB |
Output is correct |
18 |
Correct |
22 ms |
32640 KB |
Output is correct |
19 |
Correct |
21 ms |
32640 KB |
Output is correct |
20 |
Correct |
22 ms |
32632 KB |
Output is correct |
21 |
Correct |
22 ms |
32640 KB |
Output is correct |
22 |
Correct |
23 ms |
32760 KB |
Output is correct |
23 |
Correct |
22 ms |
32632 KB |
Output is correct |
24 |
Correct |
23 ms |
32640 KB |
Output is correct |
25 |
Correct |
22 ms |
32640 KB |
Output is correct |
26 |
Correct |
21 ms |
32640 KB |
Output is correct |
27 |
Correct |
22 ms |
32640 KB |
Output is correct |
28 |
Correct |
22 ms |
32640 KB |
Output is correct |
29 |
Correct |
23 ms |
32640 KB |
Output is correct |
30 |
Correct |
340 ms |
44428 KB |
Output is correct |
31 |
Correct |
305 ms |
44536 KB |
Output is correct |
32 |
Correct |
180 ms |
40440 KB |
Output is correct |
33 |
Correct |
226 ms |
50428 KB |
Output is correct |
34 |
Correct |
231 ms |
50552 KB |
Output is correct |
35 |
Correct |
234 ms |
51320 KB |
Output is correct |
36 |
Correct |
219 ms |
49784 KB |
Output is correct |
37 |
Correct |
198 ms |
45324 KB |
Output is correct |
38 |
Correct |
191 ms |
44620 KB |
Output is correct |
39 |
Correct |
194 ms |
44536 KB |
Output is correct |
40 |
Correct |
202 ms |
44408 KB |
Output is correct |
41 |
Correct |
216 ms |
44408 KB |
Output is correct |
42 |
Correct |
215 ms |
44664 KB |
Output is correct |
43 |
Correct |
323 ms |
69640 KB |
Output is correct |
44 |
Correct |
21 ms |
32640 KB |
Output is correct |
45 |
Correct |
22 ms |
32640 KB |
Output is correct |
46 |
Correct |
22 ms |
32632 KB |
Output is correct |
47 |
Correct |
22 ms |
32684 KB |
Output is correct |
48 |
Correct |
22 ms |
32640 KB |
Output is correct |
49 |
Incorrect |
22 ms |
32640 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22144' |
50 |
Halted |
0 ms |
0 KB |
- |