#include <bits/stdc++.h>
using namespace std;
using i32 = int;
using i64 = long long;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<VV<T>>;
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
#define OVERRIDE4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, l, r) for (i32 i = (i32)(l); i < (i32)(r); ++i)
#define REP(...) OVERRIDE4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, l, r) for (i32 i = (i32)(r)-1; i >= (i32)(l); --i)
#define PER(...) OVERRIDE4(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define LEN(x) (i32) size(x)
#define ALL(x) begin(x), end(x)
void dbg(i32 x) { cerr << x; }
void dbg(i64 x) { cerr << x; }
template <typename T, typename U>
void dbg(pair<T, U> p) {
cerr << "(";
dbg(p.first);
cerr << ", ";
dbg(p.second);
cerr << ")";
}
template <typename T>
void dbg(V<T> arr) {
cerr << "[";
REP(i, LEN(arr)) {
if (i) {
cerr << ", ";
}
dbg(arr[i]);
}
cerr << "]";
}
void debug() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug(Head head, Tail... tail) {
dbg(head);
cerr << ", ";
debug(tail...);
}
#ifdef DEBUGF
#define DBG(...) \
do { \
cerr << #__VA_ARGS__ << " : "; \
debug(__VA_ARGS__); \
} while (false)
#else
#define DBG(...) (void)0
#endif
#include "books.h"
i32 daitan(const V<i32> &p, i32 s) {
i32 n = LEN(p);
VV<i32> loops;
i32 root = -1;
{
V<i32> seen(n, 0);
REP(i, n) {
if (seen[i]) {
continue;
}
V<i32> tmp;
i32 cur = i;
while (!seen[cur]) {
seen[cur] = 1;
tmp.push_back(cur);
cur = p[cur];
}
if (count(ALL(tmp), s) || LEN(tmp) >= 2) {
loops.emplace_back(tmp);
if (count(ALL(tmp), s)) root = LEN(loops) - 1;
}
}
}
i32 k = LEN(loops);
constexpr i32 INF = 1001001001;
VV<i32> join(k, V<i32>(k, INF));
REP(i, k) REP(j, k) {
for (i32 x : loops[i])
for (i32 y : loops[j]) {
i32 py = p[y];
i32 tmp = abs(x - y) + abs(x - py) - abs(y - py);
chmin(join[i][j], tmp);
}
}
V<i32> rem(k, 1);
rem[root] = 0;
i32 ans = 0;
REP(iter, k - 1) {
i32 mn = INF, mnv = -1;
REP(i, k) {
if (!rem[i]) {
continue;
}
REP(j, k) {
if (rem[j]) {
continue;
}
if (chmin(mn, join[i][j])) {
mnv = i;
}
}
}
ans += mn;
rem[mnv] = 0;
}
REP(i, n) { ans += abs(i - p[i]); }
return ans;
}
i64 daitan2(const V<i32> &p, i32 s) {
i32 n = LEN(p);
V<i32> a(n, 0);
REP(i, n) {
i32 x = i, y = p[i];
if (x > y) {
swap(x, y);
}
a[x] += 1;
a[y] -= 1;
}
REP(i, n - 1) {
a[i + 1] += a[i];
}
i32 mn = s, mx = s;
REP(i, n) {
if (a[i]) {
chmin(mn, i);
chmax(mx, i + 1);
}
}
i64 ans = 0;
REP(i, n) {
ans += abs(i - p[i]);
}
REP(i, mn, mx) {
if (!a[i]) {
ans += 2;
}
}
return ans;
}
i64 minimum_walk(V<i32> p, i32 s) {
return daitan2(p, s);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
436 KB |
Output is correct |
30 |
Correct |
72 ms |
15700 KB |
Output is correct |
31 |
Correct |
75 ms |
15752 KB |
Output is correct |
32 |
Correct |
73 ms |
15556 KB |
Output is correct |
33 |
Correct |
77 ms |
15584 KB |
Output is correct |
34 |
Correct |
78 ms |
15580 KB |
Output is correct |
35 |
Correct |
80 ms |
15736 KB |
Output is correct |
36 |
Correct |
73 ms |
15636 KB |
Output is correct |
37 |
Correct |
71 ms |
15696 KB |
Output is correct |
38 |
Correct |
72 ms |
15612 KB |
Output is correct |
39 |
Correct |
72 ms |
15700 KB |
Output is correct |
40 |
Correct |
71 ms |
15696 KB |
Output is correct |
41 |
Correct |
74 ms |
15492 KB |
Output is correct |
42 |
Correct |
72 ms |
15716 KB |
Output is correct |
43 |
Correct |
71 ms |
15608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
436 KB |
Output is correct |
30 |
Correct |
72 ms |
15700 KB |
Output is correct |
31 |
Correct |
75 ms |
15752 KB |
Output is correct |
32 |
Correct |
73 ms |
15556 KB |
Output is correct |
33 |
Correct |
77 ms |
15584 KB |
Output is correct |
34 |
Correct |
78 ms |
15580 KB |
Output is correct |
35 |
Correct |
80 ms |
15736 KB |
Output is correct |
36 |
Correct |
73 ms |
15636 KB |
Output is correct |
37 |
Correct |
71 ms |
15696 KB |
Output is correct |
38 |
Correct |
72 ms |
15612 KB |
Output is correct |
39 |
Correct |
72 ms |
15700 KB |
Output is correct |
40 |
Correct |
71 ms |
15696 KB |
Output is correct |
41 |
Correct |
74 ms |
15492 KB |
Output is correct |
42 |
Correct |
72 ms |
15716 KB |
Output is correct |
43 |
Correct |
71 ms |
15608 KB |
Output is correct |
44 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |