Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/yukicoder-952.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/952

#include "../../template/template.hpp"

#include "../../structure/convex-hull-trick/convex-hull-trick-add-monotone.hpp"

int main() {
  int N;
  cin >> N;
  vector< int > A(N);
  cin >> A;
  vector< int64 > S(N + 2);
  for(int i = 0; i < N; i++) {
    S[i + 1] = S[i] + A[i];
  }

  auto dp = make_v< int64 >(N + 2, N + 1);
  auto dp2 = make_v< int64 >(N + 2, N + 1);
  fill_v(dp, infll);
  fill_v(dp2, infll);
  dp[0][0] = 0;

  vector< ConvexHullTrickAddMonotone< int64, true > > chts(2 * N + 10);
  const int Shift = N + 5;

  for(int i = 0; i <= N + 1; i++) {
    if(i <= N) {
      for(int j = 0; j <= N; j++) {
        int Q = j - i + Shift;
        if(!chts[Q].empty()) chmin(dp2[i + 1][j], S[i] * S[i] + chts[Q].query_monotone_dec(-2 * S[i]));
      }
    }
    for(int j = 0; j <= N; j++) {
      if(i) dp[i][j] = min(dp[i - 1][j], dp2[i][j]);
      if(dp[i][j] != infll) chts[j - i + Shift].add(S[i], dp[i][j] + S[i] * S[i]);
    }
  }

  for(int i = 1; i <= N; i++) {
    cout << dp[N + 1][i] << endl;
  }
}
#line 1 "test/verify/yukicoder-952.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/952

#line 1 "template/template.hpp"
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

template <typename T = int64>
vector<T> make_v(size_t a) {
  return vector<T>(a);
}

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t) fill_v(e, v);
}

template <typename F>
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(forward<F>(f)) {}

  template <typename... Args>
  decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, forward<Args>(args)...);
  }
};

template <typename F>
inline decltype(auto) MFP(F &&f) {
  return FixPoint<F>{forward<F>(f)};
}
#line 4 "test/verify/yukicoder-952.test.cpp"

#line 1 "structure/convex-hull-trick/convex-hull-trick-add-monotone.hpp"
/**
 * @brief Convex Hull Trick Add Monotone
 *
 */
template <typename T, bool isMin>
struct ConvexHullTrickAddMonotone {
#define F first
#define S second
  using P = pair<T, T>;
  deque<P> H;

  ConvexHullTrickAddMonotone() = default;

  bool empty() const { return H.empty(); }

  void clear() { H.clear(); }

  inline int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); }

  inline bool check(const P &a, const P &b, const P &c) {
    if (b.S == a.S || c.S == b.S)
      return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S);
    // return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F);
    if (is_integral<T>::value) {
      return (b.S - a.S) / (a.F - b.F) >= (c.S - b.S) / (b.F - c.F);
    } else {
      return (b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >=
             (c.F - b.F) * sgn(b.S - a.S) / abs(c.S - b.S);
    }
  }

  void add(T a, T b) {
    if (!isMin) a *= -1, b *= -1;
    P line(a, b);
    if (empty()) {
      H.emplace_front(line);
      return;
    }
    if (H.front().F <= a) {
      if (H.front().F == a) {
        if (H.front().S <= b) return;
        H.pop_front();
      }
      while (H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front();
      H.emplace_front(line);
    } else {
      assert(a <= H.back().F);
      if (H.back().F == a) {
        if (H.back().S <= b) return;
        H.pop_back();
      }
      while (H.size() >= 2 && check(H[H.size() - 2], H.back(), line))
        H.pop_back();
      H.emplace_back(line);
    }
  }

  inline T get_y(const P &a, const T &x) { return a.F * x + a.S; }

  T query(T x) {
    assert(!empty());
    int l = -1, r = H.size() - 1;
    while (l + 1 < r) {
      int m = (l + r) >> 1;
      if (get_y(H[m], x) >= get_y(H[m + 1], x))
        l = m;
      else
        r = m;
    }
    if (isMin) return get_y(H[r], x);
    return -get_y(H[r], x);
  }

  T query_monotone_inc(T x) {
    assert(!empty());
    while (H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x))
      H.pop_front();
    if (isMin) return get_y(H.front(), x);
    return -get_y(H.front(), x);
  }

  T query_monotone_dec(T x) {
    assert(!empty());
    while (H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x))
      H.pop_back();
    if (isMin) return get_y(H.back(), x);
    return -get_y(H.back(), x);
  }

#undef F
#undef S
};
#line 6 "test/verify/yukicoder-952.test.cpp"

int main() {
  int N;
  cin >> N;
  vector< int > A(N);
  cin >> A;
  vector< int64 > S(N + 2);
  for(int i = 0; i < N; i++) {
    S[i + 1] = S[i] + A[i];
  }

  auto dp = make_v< int64 >(N + 2, N + 1);
  auto dp2 = make_v< int64 >(N + 2, N + 1);
  fill_v(dp, infll);
  fill_v(dp2, infll);
  dp[0][0] = 0;

  vector< ConvexHullTrickAddMonotone< int64, true > > chts(2 * N + 10);
  const int Shift = N + 5;

  for(int i = 0; i <= N + 1; i++) {
    if(i <= N) {
      for(int j = 0; j <= N; j++) {
        int Q = j - i + Shift;
        if(!chts[Q].empty()) chmin(dp2[i + 1][j], S[i] * S[i] + chts[Q].query_monotone_dec(-2 * S[i]));
      }
    }
    for(int j = 0; j <= N; j++) {
      if(i) dp[i][j] = min(dp[i - 1][j], dp2[i][j]);
      if(dp[i][j] != infll) chts[j - i + Shift].add(S[i], dp[i][j] + S[i] * S[i]);
    }
  }

  for(int i = 1; i <= N; i++) {
    cout << dp[N + 1][i] << endl;
  }
}

Test cases

Env Name Status Elapsed Memory
g++ sample_1 :heavy_check_mark: AC 6 ms 4 MB
g++ sample_2 :heavy_check_mark: AC 6 ms 4 MB
g++ sample_3 :heavy_check_mark: AC 6 ms 4 MB
g++ testcase_1.txt :heavy_check_mark: AC 135 ms 100 MB
g++ testcase_10.txt :heavy_check_mark: AC 80 ms 59 MB
g++ testcase_11.txt :heavy_check_mark: AC 83 ms 62 MB
g++ testcase_12.txt :heavy_check_mark: AC 24 ms 17 MB
g++ testcase_13.txt :heavy_check_mark: AC 120 ms 89 MB
g++ testcase_14.txt :heavy_check_mark: AC 30 ms 21 MB
g++ testcase_15.txt :heavy_check_mark: AC 24 ms 17 MB
g++ testcase_16.txt :heavy_check_mark: AC 38 ms 28 MB
g++ testcase_17.txt :heavy_check_mark: AC 8 ms 4 MB
g++ testcase_18.txt :heavy_check_mark: AC 7 ms 4 MB
g++ testcase_19.txt :heavy_check_mark: AC 71 ms 54 MB
g++ testcase_2.txt :heavy_check_mark: AC 133 ms 98 MB
g++ testcase_20.txt :heavy_check_mark: AC 21 ms 14 MB
g++ testcase_3.txt :heavy_check_mark: AC 106 ms 80 MB
g++ testcase_4.txt :heavy_check_mark: AC 161 ms 119 MB
g++ testcase_5.txt :heavy_check_mark: AC 57 ms 42 MB
g++ testcase_6.txt :heavy_check_mark: AC 185 ms 134 MB
g++ testcase_7.txt :heavy_check_mark: AC 188 ms 136 MB
g++ testcase_8.txt :heavy_check_mark: AC 76 ms 56 MB
g++ testcase_9.txt :heavy_check_mark: AC 8 ms 4 MB
g++ testcase_tester_01.txt :heavy_check_mark: AC 7 ms 4 MB
g++ testcase_tester_02.txt :heavy_check_mark: AC 12 ms 8 MB
g++ testcase_tester_03.txt :heavy_check_mark: AC 205 ms 149 MB
clang++ sample_1 :heavy_check_mark: AC 6 ms 4 MB
clang++ sample_2 :heavy_check_mark: AC 6 ms 4 MB
clang++ sample_3 :heavy_check_mark: AC 6 ms 4 MB
clang++ testcase_1.txt :heavy_check_mark: AC 140 ms 100 MB
clang++ testcase_10.txt :heavy_check_mark: AC 82 ms 59 MB
clang++ testcase_11.txt :heavy_check_mark: AC 86 ms 62 MB
clang++ testcase_12.txt :heavy_check_mark: AC 26 ms 17 MB
clang++ testcase_13.txt :heavy_check_mark: AC 125 ms 89 MB
clang++ testcase_14.txt :heavy_check_mark: AC 31 ms 21 MB
clang++ testcase_15.txt :heavy_check_mark: AC 24 ms 17 MB
clang++ testcase_16.txt :heavy_check_mark: AC 39 ms 28 MB
clang++ testcase_17.txt :heavy_check_mark: AC 8 ms 4 MB
clang++ testcase_18.txt :heavy_check_mark: AC 7 ms 4 MB
clang++ testcase_19.txt :heavy_check_mark: AC 73 ms 54 MB
clang++ testcase_2.txt :heavy_check_mark: AC 138 ms 98 MB
clang++ testcase_20.txt :heavy_check_mark: AC 21 ms 14 MB
clang++ testcase_3.txt :heavy_check_mark: AC 110 ms 80 MB
clang++ testcase_4.txt :heavy_check_mark: AC 169 ms 119 MB
clang++ testcase_5.txt :heavy_check_mark: AC 60 ms 42 MB
clang++ testcase_6.txt :heavy_check_mark: AC 192 ms 134 MB
clang++ testcase_7.txt :heavy_check_mark: AC 196 ms 136 MB
clang++ testcase_8.txt :heavy_check_mark: AC 78 ms 56 MB
clang++ testcase_9.txt :heavy_check_mark: AC 7 ms 4 MB
clang++ testcase_tester_01.txt :heavy_check_mark: AC 7 ms 4 MB
clang++ testcase_tester_02.txt :heavy_check_mark: AC 12 ms 8 MB
clang++ testcase_tester_03.txt :heavy_check_mark: AC 210 ms 149 MB
Back to top page