Submission #1077504

# Submission time Handle Problem Language Result Execution time Memory
1077504 2024-08-27T07:44:29 Z jk_ Ancient Books (IOI17_books) C++14
100 / 100
138 ms 23136 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

struct cycle { int l, r; };

long long minimum_walk(std::vector<int> p, int s) {
  long long d = 0;
  int n = p.size();
  for (int i = 0; i < n; ++i) {
    d += abs(p[i] - i);
  }

  vector<int> c(p.size(), -1);
  vector<cycle> cycles;
  int sl=s, sr=s;
  for (int i = 0; i < n; ++i) {
    if (c[i] != -1) continue;
    if (p[i] == i) continue;
    int x = i;
    int l=x, r=x;
    c[x] = cycles.size();
    while ((x=p[x]) != i) {
      c[x] = cycles.size();
      l = min(l, x);
      r = max(r, x);
    }
    if (l <= s && r >= s){
      sl = min(l, sl);
      sr = max(r, sr);
    }
    cycles.push_back({l, r});
  }

  int l=s, r=s; // already covered
  int lx=s, rx=s; // free extension
  if (c[s] != -1) {
    lx = cycles[c[s]].l;
    rx = cycles[c[s]].r;
  }

  while (l > sl || r < sr) {
    if (l-1 >= 0 && l-1 >= lx)
      --l;
    else if (r+1 < n && r+1 <= rx)
      ++r;
    else {
      l = max(0, l-1);
      r = min(r+1, n-1);
      d += 2;
    }
    if (c[l] != -1) {
      lx = min(lx, cycles[c[l]].l);
      rx = max(rx, cycles[c[l]].r);
    }
    if (c[r] != -1) {
      lx = min(lx, cycles[c[r]].l);
      rx = max(rx, cycles[c[r]].r);
    }
  }
  while (r+1 < n) {
    ++r;
    if (c[r]==-1) continue;
    if (cycles[c[r]].l != cycles[c[r]].r) {
      d += 2*max(0, r - rx);
      rx = max(rx, cycles[c[r]].r);
    }
  }
  while (l-1 >= 0) {
    --l;
    if (c[l]==-1) continue;
    if (cycles[c[l]].l != cycles[c[l]].r) {
      d += 2*max(0, lx - l);
      lx = min(lx, cycles[c[l]].l);
    }
  }

  return d;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 432 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 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 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 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 432 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 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 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 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 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 1 ms 348 KB Output is correct
25 Correct 1 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 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 432 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 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 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 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 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 1 ms 348 KB Output is correct
25 Correct 1 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 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 106 ms 18772 KB Output is correct
31 Correct 102 ms 18768 KB Output is correct
32 Correct 97 ms 18664 KB Output is correct
33 Correct 86 ms 20948 KB Output is correct
34 Correct 81 ms 20932 KB Output is correct
35 Correct 92 ms 20960 KB Output is correct
36 Correct 82 ms 20936 KB Output is correct
37 Correct 138 ms 19284 KB Output is correct
38 Correct 82 ms 19024 KB Output is correct
39 Correct 88 ms 18772 KB Output is correct
40 Correct 126 ms 18768 KB Output is correct
41 Correct 111 ms 18772 KB Output is correct
42 Correct 122 ms 18792 KB Output is correct
43 Correct 94 ms 20968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 348 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 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 432 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 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 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 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 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 1 ms 348 KB Output is correct
25 Correct 1 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 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 106 ms 18772 KB Output is correct
31 Correct 102 ms 18768 KB Output is correct
32 Correct 97 ms 18664 KB Output is correct
33 Correct 86 ms 20948 KB Output is correct
34 Correct 81 ms 20932 KB Output is correct
35 Correct 92 ms 20960 KB Output is correct
36 Correct 82 ms 20936 KB Output is correct
37 Correct 138 ms 19284 KB Output is correct
38 Correct 82 ms 19024 KB Output is correct
39 Correct 88 ms 18772 KB Output is correct
40 Correct 126 ms 18768 KB Output is correct
41 Correct 111 ms 18772 KB Output is correct
42 Correct 122 ms 18792 KB Output is correct
43 Correct 94 ms 20968 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 1 ms 344 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 1 ms 344 KB Output is correct
64 Correct 88 ms 19924 KB Output is correct
65 Correct 92 ms 20068 KB Output is correct
66 Correct 86 ms 19172 KB Output is correct
67 Correct 88 ms 18924 KB Output is correct
68 Correct 9 ms 2256 KB Output is correct
69 Correct 12 ms 2140 KB Output is correct
70 Correct 8 ms 2396 KB Output is correct
71 Correct 11 ms 2396 KB Output is correct
72 Correct 11 ms 2396 KB Output is correct
73 Correct 10 ms 2140 KB Output is correct
74 Correct 95 ms 20956 KB Output is correct
75 Correct 78 ms 21092 KB Output is correct
76 Correct 93 ms 20956 KB Output is correct
77 Correct 97 ms 20964 KB Output is correct
78 Correct 92 ms 23016 KB Output is correct
79 Correct 94 ms 23004 KB Output is correct
80 Correct 77 ms 18672 KB Output is correct
81 Correct 91 ms 23136 KB Output is correct
82 Correct 97 ms 22980 KB Output is correct
83 Correct 90 ms 21104 KB Output is correct
84 Correct 85 ms 20968 KB Output is correct
85 Correct 83 ms 19940 KB Output is correct
86 Correct 79 ms 19392 KB Output is correct
87 Correct 87 ms 21100 KB Output is correct
88 Correct 84 ms 22952 KB Output is correct
89 Correct 84 ms 23128 KB Output is correct
90 Correct 78 ms 19304 KB Output is correct
91 Correct 82 ms 18808 KB Output is correct
92 Correct 90 ms 18772 KB Output is correct