#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define MAX 1010101
typedef pair<int, int> pii;
typedef long long ll;
int p[MAX];
ll ans = 0;
int N;
vector<vector<int>> cyc;
int cycnt;
int vis[MAX];
int chk[MAX];
int prt[MAX];
int lv[MAX];
int rv[MAX];
vector<int> adj[MAX];
vector<int> lst;
int s;
void dfs(int x) {
lst.push_back(x);
for (auto v : adj[x]) if (lv[v] <= s && s <= rv[v]) dfs(v);
}
long long minimum_walk(std::vector<int> p, int _s) {
s = _s;
N = p.size();
int i;
for (i = 0; i < N; i++) if (i != p[i]) break;
int er = i;
if (i == N) return 0;
int fcnt = 0;
for (; i < N; i++) p[fcnt++] = p[i] - er;
s -= er;
if (s < 0) ans += 2ll * (-s), s = 0;
N = fcnt;
p.resize(N);
while ((p.back() + 1) == (int)p.size()) p.pop_back();
N = p.size();
if (s >= N) ans += 2ll * (s - N + 1), s = N - 1;
for (i = 0; i < N; i++) ans += abs(p[i] - i);
for (i = 0; i < N; i++) {
if (vis[i]) continue;
vector<int> vlist;
int v = i;
while (!vis[v]) {
vis[v] = 1;
vlist.push_back(v);
v = p[v];
}
cycnt++;
cyc.push_back(vlist);
}
vector<pair<pii, int>> vpi;
int X = cyc.size();
for (auto& v : cyc) {
sort(v.begin(), v.end());
vpi.emplace_back(pii(v[0], v.back()), 0);
}
i = 0;
for (auto& [_, x] : vpi) x = i++;
sort(vpi.begin(), vpi.end());
vector<pair<pii, int>> stk;
for (i = 0; i < vpi.size(); i++) {
while (stk.size() && stk.back().first.second < vpi[i].first.first) stk.pop_back();
vector<int> er;
int t = -1;
while (stk.size() && vpi[i].first.first <= stk.back().first.second && stk.back().first.second <= vpi[i].first.second) {
er.push_back(stk.back().second);
stk.pop_back();
}
if (er.size()) {
t = er.back();
er.pop_back();
er.push_back(i);
for (auto x : er) {
chk[x] = 1;
if (cyc[t].size() < cyc[x].size()) swap(cyc[t], cyc[x]);
for (auto v : cyc[x]) cyc[t].push_back(v);
}
stk.emplace_back(pii(cyc[t][0], vpi[i].first.second), t);
}
else {
if (stk.empty()) prt[i] = -1;
else prt[i] = stk.back().second;
stk.push_back(vpi[i]);
}
}
vector<int> all;
for (i = 0; i < X; i++) {
if (!chk[i]) {
sort(cyc[i].begin(), cyc[i].end());
lv[i] = cyc[i][0];
rv[i] = cyc[i].back();
if (~prt[i]) adj[prt[i]].push_back(i);
}
}
for (i = 0; i < cyc.size(); i++) if (!~prt[i]) all.push_back(i);
for (i = 1; i < all.size(); i++) ans += 2ll * (lv[all[i]] - rv[all[i - 1]]);
int rt = -1;
for (auto v : all) {
if (lv[v] <= s && s <= rv[v]) {
rt = v;
break;
}
}
dfs(rt);
int pv = 0;
for (i = 1; i < lst.size(); i++) {
int sz = cyc[lst[pv]].size();
int ind = lower_bound(cyc[lst[pv]].begin(), cyc[lst[pv]].end(), lv[lst[i]]) - cyc[lst[pv]].begin();
int c;
if (ind == sz) c = 0;
else if (cyc[lst[pv]][ind] <= rv[lst[i]]) c = 1;
else c = 0;
if (c) continue;
else {
ind = lower_bound(cyc[lst[pv]].begin(), cyc[lst[pv]].end(), lv[lst[i]]) - cyc[lst[pv]].begin();
ind--;
int mn = 2e9;
if (0 <= ind && ind < sz) mn = min(mn, abs(cyc[lst[pv]][ind] - lv[i]));
ind = upper_bound(cyc[lst[pv]].begin(), cyc[lst[pv]].end(), rv[lst[i]]) - cyc[lst[pv]].begin();
if (0 <= ind && ind < sz) mn = min(mn, abs(cyc[lst[pv]][ind] - rv[lst[i]]));
ans += 2ll * mn;
pv = i;
}
}
return ans;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:64:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (i = 0; i < vpi.size(); i++) {
| ~~^~~~~~~~~~~~
books.cpp:98:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (i = 0; i < cyc.size(); i++) if (!~prt[i]) all.push_back(i);
| ~~^~~~~~~~~~~~
books.cpp:99:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (i = 1; i < all.size(); i++) ans += 2ll * (lv[all[i]] - rv[all[i - 1]]);
| ~~^~~~~~~~~~~~
books.cpp:109:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (i = 1; i < lst.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24020 KB |
Output is correct |
2 |
Correct |
12 ms |
24020 KB |
Output is correct |
3 |
Correct |
12 ms |
24020 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
24020 KB |
Output is correct |
8 |
Correct |
12 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24020 KB |
Output is correct |
10 |
Correct |
12 ms |
24020 KB |
Output is correct |
11 |
Correct |
13 ms |
24020 KB |
Output is correct |
12 |
Correct |
12 ms |
24020 KB |
Output is correct |
13 |
Correct |
12 ms |
24020 KB |
Output is correct |
14 |
Correct |
12 ms |
24020 KB |
Output is correct |
15 |
Correct |
14 ms |
24020 KB |
Output is correct |
16 |
Correct |
12 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
24020 KB |
Output is correct |
18 |
Correct |
12 ms |
24020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24020 KB |
Output is correct |
2 |
Correct |
12 ms |
24020 KB |
Output is correct |
3 |
Correct |
12 ms |
24020 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
24020 KB |
Output is correct |
8 |
Correct |
12 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24020 KB |
Output is correct |
10 |
Correct |
12 ms |
24020 KB |
Output is correct |
11 |
Correct |
13 ms |
24020 KB |
Output is correct |
12 |
Correct |
12 ms |
24020 KB |
Output is correct |
13 |
Correct |
12 ms |
24020 KB |
Output is correct |
14 |
Correct |
12 ms |
24020 KB |
Output is correct |
15 |
Correct |
14 ms |
24020 KB |
Output is correct |
16 |
Correct |
12 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
24020 KB |
Output is correct |
18 |
Correct |
12 ms |
24020 KB |
Output is correct |
19 |
Correct |
12 ms |
24020 KB |
Output is correct |
20 |
Correct |
11 ms |
24020 KB |
Output is correct |
21 |
Correct |
12 ms |
24020 KB |
Output is correct |
22 |
Correct |
13 ms |
24148 KB |
Output is correct |
23 |
Correct |
13 ms |
24092 KB |
Output is correct |
24 |
Correct |
13 ms |
24272 KB |
Output is correct |
25 |
Correct |
13 ms |
24020 KB |
Output is correct |
26 |
Correct |
13 ms |
24020 KB |
Output is correct |
27 |
Correct |
12 ms |
24020 KB |
Output is correct |
28 |
Correct |
12 ms |
24020 KB |
Output is correct |
29 |
Correct |
13 ms |
24052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24020 KB |
Output is correct |
2 |
Correct |
12 ms |
24020 KB |
Output is correct |
3 |
Correct |
12 ms |
24020 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
24020 KB |
Output is correct |
8 |
Correct |
12 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24020 KB |
Output is correct |
10 |
Correct |
12 ms |
24020 KB |
Output is correct |
11 |
Correct |
13 ms |
24020 KB |
Output is correct |
12 |
Correct |
12 ms |
24020 KB |
Output is correct |
13 |
Correct |
12 ms |
24020 KB |
Output is correct |
14 |
Correct |
12 ms |
24020 KB |
Output is correct |
15 |
Correct |
14 ms |
24020 KB |
Output is correct |
16 |
Correct |
12 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
24020 KB |
Output is correct |
18 |
Correct |
12 ms |
24020 KB |
Output is correct |
19 |
Correct |
12 ms |
24020 KB |
Output is correct |
20 |
Correct |
11 ms |
24020 KB |
Output is correct |
21 |
Correct |
12 ms |
24020 KB |
Output is correct |
22 |
Correct |
13 ms |
24148 KB |
Output is correct |
23 |
Correct |
13 ms |
24092 KB |
Output is correct |
24 |
Correct |
13 ms |
24272 KB |
Output is correct |
25 |
Correct |
13 ms |
24020 KB |
Output is correct |
26 |
Correct |
13 ms |
24020 KB |
Output is correct |
27 |
Correct |
12 ms |
24020 KB |
Output is correct |
28 |
Correct |
12 ms |
24020 KB |
Output is correct |
29 |
Correct |
13 ms |
24052 KB |
Output is correct |
30 |
Correct |
271 ms |
45292 KB |
Output is correct |
31 |
Correct |
267 ms |
43660 KB |
Output is correct |
32 |
Correct |
96 ms |
33440 KB |
Output is correct |
33 |
Correct |
235 ms |
106564 KB |
Output is correct |
34 |
Correct |
227 ms |
106692 KB |
Output is correct |
35 |
Correct |
221 ms |
94792 KB |
Output is correct |
36 |
Correct |
184 ms |
64240 KB |
Output is correct |
37 |
Correct |
164 ms |
45572 KB |
Output is correct |
38 |
Correct |
168 ms |
43340 KB |
Output is correct |
39 |
Correct |
173 ms |
42280 KB |
Output is correct |
40 |
Correct |
183 ms |
41932 KB |
Output is correct |
41 |
Correct |
226 ms |
41716 KB |
Output is correct |
42 |
Correct |
205 ms |
42096 KB |
Output is correct |
43 |
Correct |
263 ms |
106952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
24168 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2746' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24020 KB |
Output is correct |
2 |
Correct |
12 ms |
24020 KB |
Output is correct |
3 |
Correct |
12 ms |
24020 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
24020 KB |
Output is correct |
8 |
Correct |
12 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24020 KB |
Output is correct |
10 |
Correct |
12 ms |
24020 KB |
Output is correct |
11 |
Correct |
13 ms |
24020 KB |
Output is correct |
12 |
Correct |
12 ms |
24020 KB |
Output is correct |
13 |
Correct |
12 ms |
24020 KB |
Output is correct |
14 |
Correct |
12 ms |
24020 KB |
Output is correct |
15 |
Correct |
14 ms |
24020 KB |
Output is correct |
16 |
Correct |
12 ms |
24020 KB |
Output is correct |
17 |
Correct |
13 ms |
24020 KB |
Output is correct |
18 |
Correct |
12 ms |
24020 KB |
Output is correct |
19 |
Correct |
12 ms |
24020 KB |
Output is correct |
20 |
Correct |
11 ms |
24020 KB |
Output is correct |
21 |
Correct |
12 ms |
24020 KB |
Output is correct |
22 |
Correct |
13 ms |
24148 KB |
Output is correct |
23 |
Correct |
13 ms |
24092 KB |
Output is correct |
24 |
Correct |
13 ms |
24272 KB |
Output is correct |
25 |
Correct |
13 ms |
24020 KB |
Output is correct |
26 |
Correct |
13 ms |
24020 KB |
Output is correct |
27 |
Correct |
12 ms |
24020 KB |
Output is correct |
28 |
Correct |
12 ms |
24020 KB |
Output is correct |
29 |
Correct |
13 ms |
24052 KB |
Output is correct |
30 |
Correct |
271 ms |
45292 KB |
Output is correct |
31 |
Correct |
267 ms |
43660 KB |
Output is correct |
32 |
Correct |
96 ms |
33440 KB |
Output is correct |
33 |
Correct |
235 ms |
106564 KB |
Output is correct |
34 |
Correct |
227 ms |
106692 KB |
Output is correct |
35 |
Correct |
221 ms |
94792 KB |
Output is correct |
36 |
Correct |
184 ms |
64240 KB |
Output is correct |
37 |
Correct |
164 ms |
45572 KB |
Output is correct |
38 |
Correct |
168 ms |
43340 KB |
Output is correct |
39 |
Correct |
173 ms |
42280 KB |
Output is correct |
40 |
Correct |
183 ms |
41932 KB |
Output is correct |
41 |
Correct |
226 ms |
41716 KB |
Output is correct |
42 |
Correct |
205 ms |
42096 KB |
Output is correct |
43 |
Correct |
263 ms |
106952 KB |
Output is correct |
44 |
Incorrect |
13 ms |
24168 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2746' |
45 |
Halted |
0 ms |
0 KB |
- |