Submission #94567

# Submission time Handle Problem Language Result Execution time Memory
94567 2019-01-21T01:15:35 Z wxh010910 Ancient Books (IOI17_books) C++17
100 / 100
147 ms 19072 KB
#include <bits/stdc++.h>
#include "books.h"

using namespace std;

long long minimum_walk(vector<int> p, int s) {
  int n = p.size();
  int l = n, r = -1;
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    if (i != p[i]) {
      ans += abs(i - p[i]);
      l = min(l, i);
      r = max(r, i);
    }
  }
  int cur_l = s, cur_r = s;
  queue<int> q;
  q.push(s);
  while (true) {
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      while (cur_l > p[x]) {
        q.push(--cur_l);
      }
      while (cur_r < p[x]) {
        q.push(++cur_r);
      }
    }
    int new_l = cur_l, new_r = cur_r;
    int cost_l = 0, cost_r = 0;
    bool found_l = false, found_r = false;
    while (new_l > l && !found_l) {
      q.push(--new_l);
      ++cost_l;
      while (!q.empty()) {
        int x = q.front();
        q.pop();
        if (p[x] > s) {
          found_l = true;
        }
        while (new_l > p[x]) {
          q.push(--new_l);
        }
      }
    }
    while (new_r < r && !found_r) {
      q.push(++new_r);
      ++cost_r;
      while (!q.empty()) {
        int x = q.front();
        q.pop();
        if (p[x] < s) {
          found_r = true;
        }
        while (new_r < p[x]) {
          q.push(++new_r);
        }
      }
    }
    if (found_l && found_r) {
      ans += min(cost_l, cost_r) << 1;
      while (cur_l > new_l) {
        q.push(--cur_l);
      }
      while (cur_r < new_r) {
        q.push(++cur_r);
      }
    } else {
      ans += cost_l + cost_r << 1;
      break;
    }
  }
  return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:71:21: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
       ans += cost_l + cost_r << 1;
              ~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 364 KB Output is correct
17 Correct 2 ms 252 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 364 KB Output is correct
17 Correct 2 ms 252 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 256 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 256 KB Output is correct
27 Correct 2 ms 256 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 364 KB Output is correct
17 Correct 2 ms 252 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 256 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 256 KB Output is correct
27 Correct 2 ms 256 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 147 ms 19008 KB Output is correct
31 Correct 141 ms 19036 KB Output is correct
32 Correct 114 ms 15000 KB Output is correct
33 Correct 124 ms 14960 KB Output is correct
34 Correct 126 ms 14984 KB Output is correct
35 Correct 131 ms 14968 KB Output is correct
36 Correct 126 ms 14968 KB Output is correct
37 Correct 124 ms 14940 KB Output is correct
38 Correct 128 ms 15036 KB Output is correct
39 Correct 131 ms 14968 KB Output is correct
40 Correct 135 ms 15160 KB Output is correct
41 Correct 140 ms 16996 KB Output is correct
42 Correct 140 ms 16584 KB Output is correct
43 Correct 130 ms 18680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 252 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 380 KB Output is correct
13 Correct 2 ms 296 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 364 KB Output is correct
17 Correct 2 ms 252 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 256 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 256 KB Output is correct
27 Correct 2 ms 256 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 147 ms 19008 KB Output is correct
31 Correct 141 ms 19036 KB Output is correct
32 Correct 114 ms 15000 KB Output is correct
33 Correct 124 ms 14960 KB Output is correct
34 Correct 126 ms 14984 KB Output is correct
35 Correct 131 ms 14968 KB Output is correct
36 Correct 126 ms 14968 KB Output is correct
37 Correct 124 ms 14940 KB Output is correct
38 Correct 128 ms 15036 KB Output is correct
39 Correct 131 ms 14968 KB Output is correct
40 Correct 135 ms 15160 KB Output is correct
41 Correct 140 ms 16996 KB Output is correct
42 Correct 140 ms 16584 KB Output is correct
43 Correct 130 ms 18680 KB Output is correct
44 Correct 2 ms 252 KB Output is correct
45 Correct 2 ms 424 KB Output is correct
46 Correct 2 ms 256 KB Output is correct
47 Correct 2 ms 256 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 256 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 256 KB Output is correct
52 Correct 2 ms 252 KB Output is correct
53 Correct 2 ms 256 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 380 KB Output is correct
56 Correct 2 ms 296 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 2 ms 376 KB Output is correct
59 Correct 2 ms 256 KB Output is correct
60 Correct 2 ms 376 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 131 ms 19072 KB Output is correct
65 Correct 131 ms 18892 KB Output is correct
66 Correct 136 ms 18652 KB Output is correct
67 Correct 136 ms 18032 KB Output is correct
68 Correct 15 ms 2040 KB Output is correct
69 Correct 15 ms 2040 KB Output is correct
70 Correct 14 ms 2040 KB Output is correct
71 Correct 14 ms 2168 KB Output is correct
72 Correct 14 ms 2168 KB Output is correct
73 Correct 15 ms 1912 KB Output is correct
74 Correct 125 ms 14968 KB Output is correct
75 Correct 124 ms 14840 KB Output is correct
76 Correct 127 ms 18952 KB Output is correct
77 Correct 130 ms 18952 KB Output is correct
78 Correct 132 ms 16144 KB Output is correct
79 Correct 126 ms 16236 KB Output is correct
80 Correct 114 ms 14940 KB Output is correct
81 Correct 126 ms 16344 KB Output is correct
82 Correct 126 ms 16320 KB Output is correct
83 Correct 136 ms 15056 KB Output is correct
84 Correct 136 ms 15100 KB Output is correct
85 Correct 136 ms 14956 KB Output is correct
86 Correct 131 ms 14828 KB Output is correct
87 Correct 139 ms 14920 KB Output is correct
88 Correct 136 ms 14840 KB Output is correct
89 Correct 135 ms 14984 KB Output is correct
90 Correct 135 ms 14952 KB Output is correct
91 Correct 137 ms 15052 KB Output is correct
92 Correct 140 ms 15172 KB Output is correct