結果

問題 No.952 危険な火薬庫
ユーザー pekempeypekempey
提出日時 2019-12-15 00:30:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,594 bytes
コンパイル時間 1,728 ms
コンパイル使用メモリ 176,108 KB
実行使用メモリ 77,604 KB
最終ジャッジ日時 2023-09-10 17:49:39
合計ジャッジ時間 5,681 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

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()

struct CHT {
  struct point {
    long long 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;
    }
  };
  
  long long cross(point a, point b) {
    return a.x * b.y - 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