Submission #646337

# Submission time Handle Problem Language Result Execution time Memory
646337 2022-09-29T15:01:43 Z Alex_tz307 Peru (RMI20_peru) C++17
100 / 100
125 ms 90620 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], st[1 + kN], prv[1 + kN], nxt[1 + kN], dqMax[kN], dq[kN], stk[1 + 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) {
    prv[i] = 0;
    nxt[i] = n + 1;
    while (vf && a[st[vf]] <= a[i]) { /// <=
      nxt[st[vf]] = i;
      vf -= 1;
    }
    prv[i] = st[vf];
    st[++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 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 392 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 456 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 392 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 456 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 20 ms 13960 KB Output is correct
16 Correct 19 ms 14148 KB Output is correct
17 Correct 18 ms 14448 KB Output is correct
18 Correct 27 ms 13924 KB Output is correct
19 Correct 28 ms 13916 KB Output is correct
20 Correct 29 ms 13848 KB Output is correct
21 Correct 22 ms 14124 KB Output is correct
22 Correct 19 ms 14472 KB Output is correct
23 Correct 18 ms 14404 KB Output is correct
24 Correct 19 ms 14720 KB Output is correct
25 Correct 19 ms 14744 KB Output is correct
26 Correct 18 ms 14220 KB Output is correct
27 Correct 19 ms 13984 KB Output is correct
28 Correct 19 ms 13992 KB Output is correct
29 Correct 18 ms 14028 KB Output is correct
30 Correct 21 ms 14160 KB Output is correct
31 Correct 26 ms 14444 KB Output is correct
32 Correct 21 ms 14088 KB Output is correct
33 Correct 18 ms 14632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 13960 KB Output is correct
2 Correct 19 ms 14148 KB Output is correct
3 Correct 18 ms 14448 KB Output is correct
4 Correct 27 ms 13924 KB Output is correct
5 Correct 28 ms 13916 KB Output is correct
6 Correct 29 ms 13848 KB Output is correct
7 Correct 22 ms 14124 KB Output is correct
8 Correct 19 ms 14472 KB Output is correct
9 Correct 18 ms 14404 KB Output is correct
10 Correct 19 ms 14720 KB Output is correct
11 Correct 19 ms 14744 KB Output is correct
12 Correct 18 ms 14220 KB Output is correct
13 Correct 19 ms 13984 KB Output is correct
14 Correct 19 ms 13992 KB Output is correct
15 Correct 18 ms 14028 KB Output is correct
16 Correct 21 ms 14160 KB Output is correct
17 Correct 26 ms 14444 KB Output is correct
18 Correct 21 ms 14088 KB Output is correct
19 Correct 18 ms 14632 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 392 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 107 ms 84572 KB Output is correct
35 Correct 106 ms 83744 KB Output is correct
36 Correct 125 ms 83540 KB Output is correct
37 Correct 105 ms 84728 KB Output is correct
38 Correct 118 ms 84020 KB Output is correct
39 Correct 111 ms 84092 KB Output is correct
40 Correct 109 ms 86064 KB Output is correct
41 Correct 114 ms 86484 KB Output is correct
42 Correct 105 ms 85028 KB Output is correct
43 Correct 44 ms 33612 KB Output is correct
44 Correct 105 ms 51100 KB Output is correct
45 Correct 110 ms 51052 KB Output is correct
46 Correct 109 ms 50984 KB Output is correct
47 Correct 105 ms 85688 KB Output is correct
48 Correct 108 ms 86252 KB Output is correct
49 Correct 118 ms 85600 KB Output is correct
50 Correct 105 ms 85116 KB Output is correct
51 Correct 107 ms 85544 KB Output is correct
52 Correct 109 ms 85712 KB Output is correct
53 Correct 105 ms 85268 KB Output is correct
54 Correct 115 ms 86532 KB Output is correct
55 Correct 104 ms 82836 KB Output is correct
56 Correct 110 ms 82684 KB Output is correct
57 Correct 113 ms 82736 KB Output is correct
58 Correct 109 ms 83456 KB Output is correct
59 Correct 111 ms 83576 KB Output is correct
60 Correct 113 ms 83732 KB Output is correct
61 Correct 119 ms 90620 KB Output is correct
62 Correct 121 ms 87860 KB Output is correct
63 Correct 120 ms 88504 KB Output is correct