結果

問題 No.913 木の燃やし方
ユーザー pekempeypekempey
提出日時 2019-10-19 04:42:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 3,000 ms
コード長 1,665 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 171,640 KB
実行使用メモリ 9,496 KB
最終ジャッジ日時 2023-09-08 12:27:51
合計ジャッジ時間 9,921 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,504 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 154 ms
9,084 KB
testcase_09 AC 148 ms
8,772 KB
testcase_10 AC 148 ms
8,836 KB
testcase_11 AC 153 ms
9,176 KB
testcase_12 AC 159 ms
9,428 KB
testcase_13 AC 136 ms
9,176 KB
testcase_14 AC 130 ms
8,828 KB
testcase_15 AC 131 ms
8,832 KB
testcase_16 AC 125 ms
9,104 KB
testcase_17 AC 121 ms
8,968 KB
testcase_18 AC 120 ms
8,872 KB
testcase_19 AC 130 ms
9,344 KB
testcase_20 AC 128 ms
9,304 KB
testcase_21 AC 163 ms
9,344 KB
testcase_22 AC 154 ms
9,304 KB
testcase_23 AC 136 ms
9,368 KB
testcase_24 AC 117 ms
9,348 KB
testcase_25 AC 113 ms
9,304 KB
testcase_26 AC 139 ms
9,316 KB
testcase_27 AC 139 ms
9,304 KB
testcase_28 AC 164 ms
9,364 KB
testcase_29 AC 156 ms
9,496 KB
testcase_30 AC 164 ms
9,496 KB
testcase_31 AC 135 ms
9,424 KB
testcase_32 AC 135 ms
9,296 KB
testcase_33 AC 138 ms
9,412 KB
testcase_34 AC 128 ms
9,360 KB
testcase_35 AC 126 ms
9,400 KB
testcase_36 AC 119 ms
9,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define repr2(i, l, r) for (int i = (r) - 1; i >= (l); i--)
#define range(a) a.begin(), a.end()

using namespace std;
using ll = long long;

bool chmin(ll &x, ll y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int N; cin >> N;
  vector<ll> A(N); rep(i, N) cin >> A[i];
  vector<ll> S(N + 1);
  rep(i, N) S[i + 1] = S[i] + A[i];
  vector<ll> ans(N, 1e18);
  vector<ll> dp(N, 1e18);
  auto f = [&](ll l, ll r) -> ll {
    if (l > r) swap(l, r);
    return (r - l + 1) * (r - l + 1) + S[r + 1] - S[l];
  };
  auto efs = [&](auto efs, int l, int r, int a, int b) -> void {
    if (l > r) return;
    int k = a;
    int m = (l + r) / 2;
    rep2(i, a, b + 1) {
      if (chmin(dp[m], f(m, i))) {
        k = i;
      }
    }
    efs(efs, l, m - 1, a, k);
    efs(efs, m + 1, r, k, b);
  };
  auto dfs = [&](auto dfs, int l, int r) -> void {
    if (l == r) {
      chmin(ans[l], f(l, l));
      return;
    }
    int m = (l + r) / 2;
    dfs(dfs, l, m);
    dfs(dfs, m + 1, r);
    rep2(i, l, m + 1) dp[i] = 1e18;
    efs(efs, l, m, m + 1, r);
    rep2(i, l, m) chmin(dp[i + 1], dp[i]);
    rep2(i, l, m + 1) chmin(ans[i], dp[i]);
    rep2(i, m + 1, r + 1) dp[i] = 1e18;
    efs(efs, m + 1, r, l, m);
    repr2(i, m + 1, r) chmin(dp[i], dp[i + 1]);
    rep2(i, m + 1, r + 1) chmin(ans[i], dp[i]);
  };
  dfs(dfs, 0, N - 1);
  rep(i, N) cout << ans[i] << '\n';
}
0