結果

問題 No.913 木の燃やし方
ユーザー pekempeypekempey
提出日時 2019-10-19 00:35:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 429 ms / 3,000 ms
コード長 2,373 bytes
コンパイル時間 2,047 ms
コンパイル使用メモリ 176,528 KB
実行使用メモリ 16,180 KB
最終ジャッジ日時 2023-09-08 03:18:27
合計ジャッジ時間 15,185 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 278 ms
12,196 KB
testcase_09 AC 267 ms
11,908 KB
testcase_10 AC 267 ms
12,020 KB
testcase_11 AC 272 ms
12,028 KB
testcase_12 AC 287 ms
12,636 KB
testcase_13 AC 276 ms
12,116 KB
testcase_14 AC 267 ms
11,968 KB
testcase_15 AC 267 ms
11,880 KB
testcase_16 AC 271 ms
12,164 KB
testcase_17 AC 264 ms
12,044 KB
testcase_18 AC 264 ms
12,024 KB
testcase_19 AC 423 ms
16,024 KB
testcase_20 AC 282 ms
12,480 KB
testcase_21 AC 312 ms
12,348 KB
testcase_22 AC 334 ms
12,480 KB
testcase_23 AC 381 ms
13,104 KB
testcase_24 AC 400 ms
14,168 KB
testcase_25 AC 404 ms
16,020 KB
testcase_26 AC 288 ms
12,424 KB
testcase_27 AC 429 ms
16,180 KB
testcase_28 AC 278 ms
12,348 KB
testcase_29 AC 277 ms
12,344 KB
testcase_30 AC 295 ms
12,356 KB
testcase_31 AC 320 ms
15,472 KB
testcase_32 AC 322 ms
15,700 KB
testcase_33 AC 322 ms
15,540 KB
testcase_34 AC 308 ms
12,480 KB
testcase_35 AC 309 ms
12,488 KB
testcase_36 AC 311 ms
12,412 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;

struct point {
  ll x, y;
  point(ll x_ = 0, ll y_ = 0) : x(x_), y(y_) {}
  friend point operator-(point a, point b) {
    return point(a.x - b.x, a.y - b.y);
  }
  friend bool operator<(point a, point b) {
    return a.x != b.x ? a.x < b.x : a.y < b.y;
  }
};

ll cross(point a, point b) {
  return a.x * b.y - a.y * b.x;
}

vector<point> lower_hull(vector<point> ps) {
  const int n = ps.size();
  if (n <= 2) return ps;
  sort(ps.begin(), ps.end());
  vector<point> a;
  rep(i, ps.size()) {
    while (a.size() >= 2 && cross(a.back() - a.rbegin()[1], ps[i] - a.back()) <= 0) a.pop_back();
    a.push_back(ps[i]);
  }
  return a;
}

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

ll getmin(const vector<point> &ps, ll x) {
  int l = -1;
  int r = ps.size() - 1;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (eval(ps[m], x) <= eval(ps[m + 1], x)) {
      r = m;
    } else {
      l = m;
    }
  }
  return eval(ps[r], x);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // ----- i -----
  //    l      r
  // (r-l)^2 + s[r] - s[l]
  // r^2 + l^2 - 2*l*r + s[r] - s[l]
  // (r^2+s[r]) + (l^2 - s[l]) - 2*l*r
  // 

  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);
  auto dfs = [&](auto dfs, int l, int r) -> void {
    if (r - l == 1) {
      ans[l] = min(ans[l], A[l] + 1);
      return;
    }
    int m = (l + r) / 2;
    dfs(dfs, l, m);
    dfs(dfs, m, r);
    vector<point> L, R;
    rep2(i, l, m + 1) L.emplace_back(i, (ll)i*i - S[i]);
    rep2(i, m, r + 1) R.emplace_back(i, (ll)i*i + S[i]);
    L = lower_hull(L);
    R = lower_hull(R);
    ll mn = 1e18;
    rep2(i, l, m) {
      mn = min(mn, getmin(R, -2*i) + ((ll)i*i - S[i]));
      ans[i] = min(ans[i], mn);
    }
    mn = 1e18;
    repr2(i, m+1, r+1) {
      mn = min(mn, getmin(L, -2*i) + ((ll)i*i + S[i]));
      ans[i - 1] = min(ans[i - 1], mn);
    }
  };
  dfs(dfs, 0, N);
  rep(i, N) cout << ans[i] << '\n';
}
0