結果

問題 No.913 木の燃やし方
ユーザー 0w10w1
提出日時 2019-11-30 17:17:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 991 ms / 3,000 ms
コード長 2,126 bytes
コンパイル時間 2,386 ms
コンパイル使用メモリ 212,212 KB
実行使用メモリ 21,220 KB
最終ジャッジ日時 2024-05-01 02:10:20
合計ジャッジ時間 28,001 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 8 ms
6,940 KB
testcase_04 AC 8 ms
6,940 KB
testcase_05 AC 9 ms
6,940 KB
testcase_06 AC 7 ms
6,944 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 617 ms
8,452 KB
testcase_09 AC 593 ms
8,388 KB
testcase_10 AC 592 ms
8,320 KB
testcase_11 AC 604 ms
8,448 KB
testcase_12 AC 635 ms
8,756 KB
testcase_13 AC 609 ms
8,576 KB
testcase_14 AC 590 ms
8,320 KB
testcase_15 AC 593 ms
8,380 KB
testcase_16 AC 607 ms
8,576 KB
testcase_17 AC 579 ms
8,192 KB
testcase_18 AC 585 ms
8,320 KB
testcase_19 AC 850 ms
21,212 KB
testcase_20 AC 619 ms
8,732 KB
testcase_21 AC 639 ms
8,836 KB
testcase_22 AC 659 ms
9,100 KB
testcase_23 AC 711 ms
10,776 KB
testcase_24 AC 770 ms
14,792 KB
testcase_25 AC 833 ms
21,220 KB
testcase_26 AC 637 ms
8,736 KB
testcase_27 AC 991 ms
21,076 KB
testcase_28 AC 632 ms
8,732 KB
testcase_29 AC 639 ms
8,752 KB
testcase_30 AC 631 ms
8,640 KB
testcase_31 AC 769 ms
21,008 KB
testcase_32 AC 777 ms
21,128 KB
testcase_33 AC 779 ms
21,140 KB
testcase_34 AC 639 ms
8,836 KB
testcase_35 AC 638 ms
8,844 KB
testcase_36 AC 638 ms
8,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct LineFunc {
  mutable int64_t m, a, p;
  bool operator<(const LineFunc& l) const { return m < l.m; }
  bool operator<(int64_t x) const { return p < x; }
  int64_t eval(int64_t x) const { return m * x + a; }
};

struct LineContainer : multiset<LineFunc, less<>> {
  const int64_t inf = numeric_limits<int64_t>::max();
  int64_t fdiv(int64_t a, int64_t b) { return a / b - ((a ^ b) < 0 && a % b); }
  bool isect(iterator x, iterator y) {
    if (y == end()) return x->p = inf, false;
    if (x->m == y->m) x->p = x->a > y->a ? inf : -inf;
    else x->p = fdiv(y->a - x->a, x->m - y->m);
    return x->p >= y->p;
  }
  void add(int64_t m, int64_t a) {
    auto z = insert({ m, a, 0 }), y = z++, x = y;
    while (isect(y, z)) z = erase(z);
    if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
    while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y));
  }
  int64_t query(int64_t x) { return lower_bound(x)->eval(x); }
};

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  vector<int64_t> ans(N, (int64_t) 1 << 60);
  for (int _ = 0; _ < 2; ++_) {
    vector<int64_t> pa(N + 1);
    for (int i = 0; i < N; ++i) {
      pa[i + 1] = pa[i] + A[i];
    }

    function<void(int, int)> dvcq = [&](int lb, int rb) {
      if (rb - lb == 1) {
        ans[lb] = min<int64_t>(ans[lb], 1 + A[lb]);
        return;
      }
      int mb = lb + rb + _ >> 1;
      LineContainer lcn;
      for (int i = mb + 1; i <= rb; ++i) lcn.add(2 * i, -(pa[i] + (int64_t) i * i));
      vector<int64_t> best(mb - lb, (int64_t) 1 << 60);
      for (int i = lb; i < mb; ++i) best[i - lb] = -lcn.query(i) + (int64_t) i * i - pa[i];
      for (int i = 1; i < mb - lb; ++i) best[i] = min(best[i], best[i - 1]);
      for (int i = lb; i < mb; ++i) ans[i] = min(ans[i], best[i - lb]);
      dvcq(lb, mb);
      dvcq(mb, rb);
    };

    dvcq(0, N);

    reverse(A.begin(), A.end());
    reverse(ans.begin(), ans.end());
  }

  for (int i = 0; i < N; ++i) cout << ans[i] << endl;

  return 0;
}
0