Submission #646342

# Submission time Handle Problem Language Result Execution time Memory
646342 2022-09-29T15:12:11 Z Alex_tz307 Peru (RMI20_peru) C++17
100 / 100
115 ms 66660 KB
#include <bits/stdc++.h>
#include "peru.h"

using namespace std;

const int base = 23;
const int mod = 1e9 + 7;
const int kN = 2e6 + 5e5;
const int64_t INF = 1e18L;

int a[1 + kN], stk[1 + kN], prv[1 + kN], nxt[1 + kN], dq[kN], dqMax[kN];
int64_t dp[1 + kN];

void minSelf(int64_t &x, int64_t y) {
  if (y < x) {
    x = y;
  }
}

int64_t cost(int index) {
  return dp[prv[index]] + a[index];
}

int add(int x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
  return x;
}

int mult(int x, int y) {
  return (int64_t)x * y % mod;
}

int solve(int n, int k, int* v) {
  for (int i = 1; i <= n; ++i) {
    a[i] = v[i - 1];
  }

  int vf = 0;
  for (int i = 1; i <= n; ++i) {
    nxt[i] = n + 1;

    while (vf && a[stk[vf]] <= a[i]) { /// <=
      nxt[stk[vf]] = i;
      vf -= 1;
    }

    prv[i] = stk[vf];
    stk[++vf] = i;
  }

  int res = 0, l = 0, r = -1, lmax = 0, rmax = -1;
  vf = 0;
  for (int i = 1; i <= n; ++i) {
    while (vf && a[stk[vf]] <= a[i]) { /// <=
      vf -= 1;
    }

    if (nxt[i] - prv[i] >= k) {
      while (l <= r && cost(i) <= cost(dq[r])) {
        r -= 1;
      }
      dq[++r] = i;
    } else if (vf == 0 || cost(i) < cost(stk[vf])) {
      stk[++vf] = i;
    }

    while (l <= r && prv[dq[l]] <= i - k) {
      l += 1;
    }

    while (lmax <= rmax && a[dqMax[rmax]] <= a[i]) {
      rmax -= 1;
    }
    dqMax[++rmax] = i;
    while (dqMax[lmax] <= i - k) {
      lmax += 1;
    }

    if (i >= k) {
      dp[i] = dp[i - k] + a[dqMax[lmax]];
    } else {
      dp[i] = a[dqMax[lmax]];
    }
    if (l <= r) {
      minSelf(dp[i], cost(dq[l]));
    }
    if (vf) {
      minSelf(dp[i], cost(stk[vf]));
    }

    res = add(mult(res, base), dp[i] % mod);
  }

  return res;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 17 ms 9940 KB Output is correct
16 Correct 17 ms 10000 KB Output is correct
17 Correct 18 ms 10324 KB Output is correct
18 Correct 25 ms 9804 KB Output is correct
19 Correct 25 ms 9808 KB Output is correct
20 Correct 24 ms 9808 KB Output is correct
21 Correct 15 ms 10068 KB Output is correct
22 Correct 19 ms 10272 KB Output is correct
23 Correct 17 ms 10288 KB Output is correct
24 Correct 19 ms 10708 KB Output is correct
25 Correct 17 ms 10696 KB Output is correct
26 Correct 18 ms 10156 KB Output is correct
27 Correct 19 ms 9876 KB Output is correct
28 Correct 17 ms 9940 KB Output is correct
29 Correct 18 ms 9940 KB Output is correct
30 Correct 18 ms 10036 KB Output is correct
31 Correct 17 ms 10376 KB Output is correct
32 Correct 18 ms 9984 KB Output is correct
33 Correct 17 ms 10496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 9940 KB Output is correct
2 Correct 17 ms 10000 KB Output is correct
3 Correct 18 ms 10324 KB Output is correct
4 Correct 25 ms 9804 KB Output is correct
5 Correct 25 ms 9808 KB Output is correct
6 Correct 24 ms 9808 KB Output is correct
7 Correct 15 ms 10068 KB Output is correct
8 Correct 19 ms 10272 KB Output is correct
9 Correct 17 ms 10288 KB Output is correct
10 Correct 19 ms 10708 KB Output is correct
11 Correct 17 ms 10696 KB Output is correct
12 Correct 18 ms 10156 KB Output is correct
13 Correct 19 ms 9876 KB Output is correct
14 Correct 17 ms 9940 KB Output is correct
15 Correct 18 ms 9940 KB Output is correct
16 Correct 18 ms 10036 KB Output is correct
17 Correct 17 ms 10376 KB Output is correct
18 Correct 18 ms 9984 KB Output is correct
19 Correct 17 ms 10496 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 105 ms 62812 KB Output is correct
35 Correct 111 ms 61544 KB Output is correct
36 Correct 102 ms 61668 KB Output is correct
37 Correct 110 ms 62744 KB Output is correct
38 Correct 105 ms 61952 KB Output is correct
39 Correct 107 ms 62032 KB Output is correct
40 Correct 109 ms 64064 KB Output is correct
41 Correct 102 ms 64388 KB Output is correct
42 Correct 112 ms 62964 KB Output is correct
43 Correct 39 ms 24140 KB Output is correct
44 Correct 92 ms 35644 KB Output is correct
45 Correct 91 ms 35732 KB Output is correct
46 Correct 94 ms 35644 KB Output is correct
47 Correct 101 ms 61572 KB Output is correct
48 Correct 110 ms 62248 KB Output is correct
49 Correct 99 ms 61636 KB Output is correct
50 Correct 106 ms 61048 KB Output is correct
51 Correct 102 ms 61348 KB Output is correct
52 Correct 107 ms 61404 KB Output is correct
53 Correct 115 ms 61036 KB Output is correct
54 Correct 106 ms 64588 KB Output is correct
55 Correct 100 ms 60616 KB Output is correct
56 Correct 113 ms 60616 KB Output is correct
57 Correct 113 ms 60604 KB Output is correct
58 Correct 103 ms 61432 KB Output is correct
59 Correct 112 ms 61568 KB Output is correct
60 Correct 109 ms 61636 KB Output is correct
61 Correct 107 ms 66660 KB Output is correct
62 Correct 112 ms 64088 KB Output is correct
63 Correct 110 ms 64900 KB Output is correct