結果

問題 No.952 危険な火薬庫
ユーザー pekempeypekempey
提出日時 2019-12-15 00:33:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 422 ms / 2,000 ms
コード長 1,622 bytes
コンパイル時間 1,987 ms
コンパイル使用メモリ 176,040 KB
実行使用メモリ 77,828 KB
最終ジャッジ日時 2023-09-10 17:52:44
合計ジャッジ時間 6,517 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 297 ms
52,604 KB
testcase_04 AC 294 ms
51,916 KB
testcase_05 AC 232 ms
42,656 KB
testcase_06 AC 372 ms
62,188 KB
testcase_07 AC 112 ms
23,620 KB
testcase_08 AC 422 ms
70,180 KB
testcase_09 AC 420 ms
71,176 KB
testcase_10 AC 151 ms
30,300 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 163 ms
32,052 KB
testcase_13 AC 171 ms
33,596 KB
testcase_14 AC 37 ms
10,528 KB
testcase_15 AC 266 ms
47,140 KB
testcase_16 AC 48 ms
12,800 KB
testcase_17 AC 37 ms
10,528 KB
testcase_18 AC 67 ms
16,132 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 148 ms
29,248 KB
testcase_22 AC 29 ms
9,084 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 14 ms
5,980 KB
testcase_25 AC 391 ms
77,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

using i128 = __int128_t;

struct CHT {
  struct point {
    i128 x, y;
    point(long long x_ = 0, long long y_ = 0) : x(x_), y(y_) {}

    friend point operator-(point a, point b) {
      a.x -= b.x;
      a.y -= b.y;
      return a;
    }
  };
  
  i128 cross(point a, point b) {
    return (i128)a.x * b.y - (i128)a.y * b.x;
  }

  deque<point> H;

  long long eval(point a, long long x) {
    return a.x * x + a.y;
  }

  void push(long long x, long long y) {
    point p(x, y);
    while (H.size() >= 2 && cross(H[H.size() - 1] - H[H.size() - 2], p - H[H.size() - 1]) >= 0) {
      H.pop_back();
    }
    H.push_back(p);
  }

  long long maximum(long long x) {
    if (H.empty()) return -1e18;
    while (H.size() >= 2 && eval(H[0], x) <= eval(H[1], x)) H.pop_front();
    return eval(H[0], x);
  }
};

int main() {
  int N; cin >> N;
  vector<ll> A(N + 1); rep(i, N) cin >> A[i];
  vector<ll> S(N + 2); rep(i, N + 1) S[i + 1] = S[i] + A[i];
  vector<vector<ll>> dp(N + 2, vector<ll>(N + 2, 1e18));
  dp[0][0] = 0;
  vector<CHT> cht(N + 2);
  constexpr ll inf = 1e18;
  cht[0].push(0, 0);
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      ll v = -cht[j].maximum(S[i]);
      if (v == inf) continue;
      dp[i + 1][j + 1] = v + S[i] * S[i];
      cht[j + 1].push(2*S[i+1], -dp[i+1][j+1] - S[i+1]*S[i+1]);
    }
  }
  for (int i = N; i >= 1; i--) {
    cout << dp[N+1][i] << endl;
  }
}
0