// ~ Be Name Khoda ~ //
#include "books.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 1e6 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
ll ans = 0;
int per[maxn5], req[maxn5], cmp[maxn5], ps[maxn5];
int num = 0, av[maxn5], cnt = 0;
bool mark[maxn5];
void dfs(int v){
mark[v] = true;
req[min(v, per[v])]++;
req[max(v, per[v])]--;
ans += abs(v - per[v]);
cnt++;
cmp[v] = num;
if(!mark[per[v]])
dfs(per[v]);
}
int get(int l, int r){
if(l > r)
return 0;
return ps[r] - (l ? ps[l - 1] : 0);
}
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
int mx = 0;
for(int i = 0; i < n; i++){
per[i] = p[i];
if(i != p[i])
mx = i;
}
int good = 0;
fill(av, av + n + 5, n + 10);
for(int i = 0; i < n; i++) if(!mark[i]){
cnt = 0;
dfs(i);
if(cnt > 1)
good++;
num++;
}
int emp = 0, cur = 0;
for(int i = 0; i < n; i++){
cur += req[i];
if(i)
ps[i] = ps[i - 1];
if(!cur)
ps[i]++;
}
memset(req, 0, sizeof req);
int ok = 0;
int mn = 2 * n;
for(int l = s; l >= 0; l--){
req[cmp[l]]++;
if(p[l] != l && req[cmp[l]] == 1)
ok++;
for(int r = s; r < n; r++){
req[cmp[r]]++;
if(p[r] != r && req[cmp[r]] == 1)
ok++;
if(ok == good)
mn = min(mn, 2 * get(l, r - 1));
}
for(int r = s; r < n; r++){
req[cmp[r]]--;
if(p[r] != r && !req[cmp[r]])
ok--;
}
}
return ans + mn;
return ans + 2 * get(0, mx - 1);
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:67:6: warning: unused variable 'emp' [-Wunused-variable]
67 | int emp = 0, cur = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4180 KB |
Output is correct |
18 |
Correct |
1 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4180 KB |
Output is correct |
18 |
Correct |
1 ms |
4180 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
21 |
Correct |
2 ms |
4180 KB |
Output is correct |
22 |
Correct |
2 ms |
4180 KB |
Output is correct |
23 |
Correct |
2 ms |
4224 KB |
Output is correct |
24 |
Correct |
2 ms |
4180 KB |
Output is correct |
25 |
Correct |
2 ms |
4180 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
4180 KB |
Output is correct |
28 |
Correct |
2 ms |
4180 KB |
Output is correct |
29 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4180 KB |
Output is correct |
18 |
Correct |
1 ms |
4180 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
21 |
Correct |
2 ms |
4180 KB |
Output is correct |
22 |
Correct |
2 ms |
4180 KB |
Output is correct |
23 |
Correct |
2 ms |
4224 KB |
Output is correct |
24 |
Correct |
2 ms |
4180 KB |
Output is correct |
25 |
Correct |
2 ms |
4180 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
4180 KB |
Output is correct |
28 |
Correct |
2 ms |
4180 KB |
Output is correct |
29 |
Correct |
2 ms |
4180 KB |
Output is correct |
30 |
Correct |
144 ms |
28600 KB |
Output is correct |
31 |
Correct |
121 ms |
28656 KB |
Output is correct |
32 |
Correct |
85 ms |
28656 KB |
Output is correct |
33 |
Correct |
116 ms |
28544 KB |
Output is correct |
34 |
Correct |
97 ms |
28544 KB |
Output is correct |
35 |
Correct |
109 ms |
28664 KB |
Output is correct |
36 |
Correct |
91 ms |
28660 KB |
Output is correct |
37 |
Correct |
86 ms |
28664 KB |
Output is correct |
38 |
Correct |
110 ms |
28620 KB |
Output is correct |
39 |
Correct |
93 ms |
28548 KB |
Output is correct |
40 |
Correct |
101 ms |
28616 KB |
Output is correct |
41 |
Correct |
101 ms |
28656 KB |
Output is correct |
42 |
Correct |
100 ms |
28548 KB |
Output is correct |
43 |
Correct |
121 ms |
28656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4180 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 |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4180 KB |
Output is correct |
18 |
Correct |
1 ms |
4180 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
21 |
Correct |
2 ms |
4180 KB |
Output is correct |
22 |
Correct |
2 ms |
4180 KB |
Output is correct |
23 |
Correct |
2 ms |
4224 KB |
Output is correct |
24 |
Correct |
2 ms |
4180 KB |
Output is correct |
25 |
Correct |
2 ms |
4180 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
4180 KB |
Output is correct |
28 |
Correct |
2 ms |
4180 KB |
Output is correct |
29 |
Correct |
2 ms |
4180 KB |
Output is correct |
30 |
Correct |
144 ms |
28600 KB |
Output is correct |
31 |
Correct |
121 ms |
28656 KB |
Output is correct |
32 |
Correct |
85 ms |
28656 KB |
Output is correct |
33 |
Correct |
116 ms |
28544 KB |
Output is correct |
34 |
Correct |
97 ms |
28544 KB |
Output is correct |
35 |
Correct |
109 ms |
28664 KB |
Output is correct |
36 |
Correct |
91 ms |
28660 KB |
Output is correct |
37 |
Correct |
86 ms |
28664 KB |
Output is correct |
38 |
Correct |
110 ms |
28620 KB |
Output is correct |
39 |
Correct |
93 ms |
28548 KB |
Output is correct |
40 |
Correct |
101 ms |
28616 KB |
Output is correct |
41 |
Correct |
101 ms |
28656 KB |
Output is correct |
42 |
Correct |
100 ms |
28548 KB |
Output is correct |
43 |
Correct |
121 ms |
28656 KB |
Output is correct |
44 |
Incorrect |
3 ms |
4180 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |