Submission #318599

# Submission time Handle Problem Language Result Execution time Memory
318599 2020-11-02T12:35:17 Z Lemur95 Ancient Books (IOI17_books) C++17
42 / 100
1531 ms 1048576 KB
#include <bits/stdc++.h>
#include "books.h"
#pragma GCC optimize("Ofast")
#define x first
#define y second
#define ld long double
#define ll long long

using namespace std;

int n, s, x;

map <pair <int, int>, int> calc;

void f(int &l, int &r, vector <int> L, vector <int> R, vector <int> cyc) {
  int st = min(l, min(L[cyc[l]], L[cyc[r]])), dr = max(r, max(R[cyc[l]], R[cyc[r]]));
  while(st < l || r < dr) {
    if(st < l) {
      l--;
      st = min(st, L[cyc[l]]);
      dr = max(dr, R[cyc[l]]);
    } else {
      r++;
      st = min(st, L[cyc[r]]);
      dr = max(dr, R[cyc[r]]);
    }
  }
}

int dp(int l, int r, vector <int> L, vector <int> R, vector <int> cyc) {
  f(l, r, L, R, cyc);
  if(calc.find({l, r}) != calc.end())
    return calc[{l, r}];
  int ans = (int)1e9;
  int st, dr;
  if(l) {
    st = l - 1; dr = r;
    f(st, dr, L, R, cyc);
    ans = min(ans, 2 + dp(st, dr, L, R, cyc));
  }
  if(r < cyc.size() - 1) {
    st = l; dr = r + 1;
    f(st, dr, L, R, cyc);
    ans = min(ans, 2 + dp(st, dr, L, R, cyc));
  }
  calc[{l, r}] = ans;
  return ans;
}

ll minimum_walk(vector <int> p, int s) {
  ll ans = 0;
  int n = p.size(), c = 0, st = s, dr = s;
  vector <int> cyc(n + 5), l(n + 5), r(n + 5);
  fill(cyc.begin(), cyc.end(), -1);
  calc.clear();
  for(int i = 0; i < n; i++) {
    ans += abs(i - p[i]);
    if(cyc[i] == -1) {
      l[c] = r[c] = i;
      int j = i;
      while(cyc[j] == -1) {
        cyc[j] = c;
        r[c] = max(r[c], j);
        j = p[j];
      }
      if(i != p[i]) {
        st = min(st, l[c]);
        dr = max(dr, r[c]);
      }
      c++;
    }
  }
  calc[{st, dr}] = 0;
  return ans + dp(s, s, l, r, cyc);
}

/*int main() {
  //ios_base :: sync_with_stdio(false);
  //cin.tie(0); cout.tie(0);
  cin >> n >> s;
  vector <int> p;
  for(int i = 1; i <= n; i++)
    cin >> x, p.push_back(x);
  cout << minimum_walk(p, s);
  return 0;
}

*/

Compilation message

books.cpp: In function 'int dp(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
books.cpp:41:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |   if(r < cyc.size() - 1) {
      |      ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 6 ms 8172 KB Output is correct
23 Correct 6 ms 7916 KB Output is correct
24 Correct 4 ms 5484 KB Output is correct
25 Correct 4 ms 4720 KB Output is correct
26 Correct 1 ms 1004 KB Output is correct
27 Correct 1 ms 748 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 6 ms 8172 KB Output is correct
23 Correct 6 ms 7916 KB Output is correct
24 Correct 4 ms 5484 KB Output is correct
25 Correct 4 ms 4720 KB Output is correct
26 Correct 1 ms 1004 KB Output is correct
27 Correct 1 ms 748 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 219 ms 50148 KB Output is correct
31 Correct 224 ms 50148 KB Output is correct
32 Correct 163 ms 50148 KB Output is correct
33 Runtime error 1531 ms 1048576 KB Execution killed with signal 9 (could be triggered by violating memory limits)
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 832 ms 11720 KB Output is correct
2 Correct 6 ms 1772 KB Output is correct
3 Correct 245 ms 5936 KB Output is correct
4 Correct 640 ms 9988 KB Output is correct
5 Correct 700 ms 10580 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 2 ms 876 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 42 ms 5100 KB Output is correct
12 Correct 44 ms 5740 KB Output is correct
13 Correct 4 ms 2156 KB Output is correct
14 Correct 3 ms 1260 KB Output is correct
15 Correct 1 ms 748 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 12 ms 6124 KB Output is correct
18 Correct 11 ms 5484 KB Output is correct
19 Correct 6 ms 2540 KB Output is correct
20 Correct 1 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 6 ms 8172 KB Output is correct
23 Correct 6 ms 7916 KB Output is correct
24 Correct 4 ms 5484 KB Output is correct
25 Correct 4 ms 4720 KB Output is correct
26 Correct 1 ms 1004 KB Output is correct
27 Correct 1 ms 748 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 219 ms 50148 KB Output is correct
31 Correct 224 ms 50148 KB Output is correct
32 Correct 163 ms 50148 KB Output is correct
33 Runtime error 1531 ms 1048576 KB Execution killed with signal 9 (could be triggered by violating memory limits)
34 Halted 0 ms 0 KB -