結果

問題 No.2332 Make a Sequence
ユーザー 👑 emthrmemthrm
提出日時 2023-05-28 16:34:19
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,281 bytes
コンパイル時間 4,360 ms
コンパイル使用メモリ 261,152 KB
実行使用メモリ 21,344 KB
最終ジャッジ日時 2023-08-27 14:02:52
合計ジャッジ時間 16,265 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 WA -
testcase_08 AC 24 ms
4,700 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 145 ms
20,252 KB
testcase_48 AC 145 ms
20,984 KB
testcase_49 AC 146 ms
20,256 KB
testcase_50 AC 147 ms
20,608 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 AC 122 ms
20,388 KB
testcase_58 AC 122 ms
20,432 KB
testcase_59 AC 122 ms
20,328 KB
testcase_60 AC 123 ms
20,316 KB
testcase_61 AC 123 ms
20,244 KB
testcase_62 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
std::vector<int> z_algorithm(const T &s) {
  const int n = s.size();
  std::vector<int> res(n, 0);
  for (int i = 1, j = 0; i < n; ++i) {
    if (i + res[i - j] < j + res[j]) {
      res[i] = res[i - j];
    } else {
      res[i] = std::max(j + res[j] - i, 0);
      while (i + res[i] < n && s[i + res[i]] == s[res[i]]) ++res[i];
      j = i;
    }
  }
  res[0] = n;
  return res;
}

template <typename T, bool IS_MINIMIZED = true>
struct LiChaoTree {
  struct Line {
    T a, b;
    explicit Line(const T a, const T b) : a(a), b(b) {}
    T f(const T x) const { return a * x + b; }
  };

  explicit LiChaoTree(const std::vector<T>& xs_, const T inf) : n(1), xs(xs_) {
    std::sort(xs.begin(), xs.end());
    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
    assert(xs.size() > 0);
    n = std::bit_ceil(xs.size());
    const T xs_back = xs.back();
    xs.resize(n, xs_back);
    dat.assign(n << 1, Line(0, inf));
  }

  void add(T a, T b) {
    if constexpr (!IS_MINIMIZED) {
      a = -a;
      b = -b;
    }
    Line line(a, b);
    add(&line, 1, 0, n);
  }

  void add(T a, T b, T left, T right) {
    if constexpr (!IS_MINIMIZED) {
      a = -a;
      b = -b;
    }
    for (int len = 1,
             node_l = std::distance(
                 xs.begin(), std::lower_bound(xs.begin(), xs.end(), left)),
             node_r = std::distance(
                 xs.begin(), std::lower_bound(xs.begin(), xs.end(), right)),
             l = node_l + n, r = node_r + n;
         l < r;
         l >>= 1, r >>= 1, len <<= 1) {
      if (l & 1) {
        Line line(a, b);
        add(&line, l++, node_l, node_l + len);
        node_l += len;
      }
      if (r & 1) {
        Line line(a, b);
        node_r -= len;
        add(&line, --r, node_r, node_r + len);
      }
    }
  }

  T query(const T x) const {
    int node = n + std::distance(xs.begin(),
                                 std::lower_bound(xs.begin(), xs.end(), x));
    T res = dat[node].f(x);
    while (node >>= 1) {
      if (dat[node].f(x) < res) res = dat[node].f(x);
    }
    return IS_MINIMIZED ? res : -res;
  }

 private:
  int n;
  std::vector<T> xs;
  std::vector<Line> dat;

  void add(Line* line, int node, int left, int right) {
    const bool flag_l = dat[node].f(xs[left]) <= line->f(xs[left]);
    const bool flag_r = dat[node].f(xs[right - 1]) <= line->f(xs[right - 1]);
    if (flag_l && flag_r) return;
    if (!flag_l && !flag_r) {
      std::swap(dat[node], *line);
      return;
    }
    const int mid = std::midpoint(left, right);
    if (line->f(xs[mid]) < dat[node].f(xs[mid])) std::swap(dat[node], *line);
    if (line->f(xs[left]) <= dat[node].f(xs[left])) {
      add(line, node << 1, left, mid);
    } else {
      add(line, (node << 1) + 1, mid, right);
    }
  }
};

int main() {
  int n, m; cin >> n >> m;
  vector<int> a(n + m + 1, -1), c(m);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> a[n + 1 + i];
  REP(i, m) cin >> c[i];
  const vector<int> z = z_algorithm(a);
  vector<ll> queries(m);
  iota(ALL(queries), 1);
  LiChaoTree<ll> li_chao_tree(queries, LINF);
  vector<ll> dp(m + 1, LINF);
  dp[0] = 0;
  REP(i, m + 1) {
    if (i > 0) chmin(dp[i], li_chao_tree.query(i));
    if (i < m) li_chao_tree.add(c[i], dp[i] - i * c[i], i + 1, i + z[n + 1 + i] + 1);
  }
  // REP(i, m + 1) cout << dp[i] << " \n"[i == m];
  cout << (dp[m] == LINF ? -1 : dp[m]) << '\n';
  return 0;
}
0