結果

問題 No.952 危険な火薬庫
ユーザー 👑 emthrmemthrm
提出日時 2019-12-16 14:12:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 4,255 bytes
コンパイル時間 1,448 ms
コンパイル使用メモリ 131,984 KB
実行使用メモリ 73,572 KB
最終ジャッジ日時 2023-09-15 17:36:01
合計ジャッジ時間 4,818 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 183 ms
49,812 KB
testcase_04 AC 181 ms
48,648 KB
testcase_05 AC 145 ms
39,688 KB
testcase_06 AC 223 ms
59,112 KB
testcase_07 AC 67 ms
21,456 KB
testcase_08 AC 253 ms
66,788 KB
testcase_09 AC 264 ms
67,484 KB
testcase_10 AC 93 ms
28,060 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 100 ms
29,648 KB
testcase_13 AC 109 ms
30,960 KB
testcase_14 AC 22 ms
9,156 KB
testcase_15 AC 160 ms
44,164 KB
testcase_16 AC 29 ms
11,176 KB
testcase_17 AC 22 ms
9,452 KB
testcase_18 AC 41 ms
14,364 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 89 ms
27,088 KB
testcase_22 AC 18 ms
7,788 KB
testcase_23 AC 3 ms
4,384 KB
testcase_24 AC 7 ms
4,884 KB
testcase_25 AC 248 ms
73,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
template <typename T>
struct CHT {
  using Line = pair<T, T>;

  CHT(bool is_minimized = true) : is_minimized(is_minimized) {}

  void add(T a, T b) {
    Line now(a, b);
    if (deq.empty()) {
      deq.emplace_back(now);
    } else if (deq.back().first <= a) {
      if (deq.back().first == a) {
        if (is_minimized) {
          if (b >= deq.back().second) return;
          deq.pop_back();
        } else {
          if (b <= deq.back().second) return;
          deq.pop_back();
        }
      }
      while (deq.size() >= 2 && !is_necessary(deq[deq.size() - 2], deq.back(), now)) deq.pop_back();
      deq.emplace_back(now);
    } else {
      if (deq.front().first == a) {
        if (is_minimized) {
          if (b >= deq.front().second) return;
          deq.pop_front();
        } else {
          if (b <= deq.front().second) return;
          deq.pop_front();
        }
      }
      while (deq.size() >= 2 && !is_necessary(now, deq[0], deq[1])) deq.pop_front();
      deq.emplace_front(now);
    }
  }

  T query(T x) {
    int lb = -1, ub = deq.size() - 1;
    while (ub - lb > 1) {
      int mid = (lb + ub) >> 1;
      if (is_minimized) {
        (f(deq[mid], x) < f(deq[mid + 1], x) ? ub : lb) = mid;
      } else {
        (f(deq[mid], x) > f(deq[mid + 1], x) ? ub : lb) = mid;
      }
    }
    return f(deq[ub], x);
  }

  T monotone_inc_query(T x) {
    if (is_minimized) {
      while (deq.size() >= 2 && f(deq[deq.size() - 2], x) <= f(deq.back(), x)) deq.pop_back();
      return f(deq.back(), x);
    } else {
      while (deq.size() >= 2 && f(deq[0], x) <= f(deq[1], x)) deq.pop_front();
      return f(deq.front(), x);
    }
  }

  T monotone_dec_query(T x) {
    if (is_minimized) {
      while (deq.size() >= 2 && f(deq[0], x) >= f(deq[1], x)) deq.pop_front();
      return f(deq.front(), x);
    } else {
      while (deq.size() >= 2 && f(deq[deq.size() - 2], x) >= f(deq.back(), x)) deq.pop_back();
      return f(deq.back(), x);
    }
  }

private:
  bool is_minimized;
  deque<Line> deq;

  using Real = long double;
  bool is_necessary(const Line &l1, const Line &l2, const Line &l3) {
    Real lhs = static_cast<Real>(l3.second - l2.second) / (l2.first - l3.first);
    Real rhs = static_cast<Real>(l2.second - l1.second) / (l1.first - l2.first);
    // T lhs = (l1.first - l2.first) * (l3.second - l2.second);
    // T rhs = (l2.first - l3.first) * (l2.second - l1.second);
    return is_minimized ? lhs < rhs : lhs > rhs;
  }

  T f(const Line &l, T x) { return l.first * x + l.second; }
};

int main() {
  int n; cin >> n;
  vector<long long> a(n + 1, 0); FOR(i, 1, n + 1) cin >> a[i];
  FOR(i, 2, n + 1) a[i] += a[i - 1];
  vector<vector<long long> > dp(n + 2, vector<long long>(n + 1, -LINF));
  dp[0][0] = 0;
  FOR(i, 1, n + 2) dp[i][i - 1] = (a[i - 1] - a[0]) * (a[i - 1] - a[0]);
  FOR(i, 1, n + 1) {
    CHT<long long> cht;
    for (int k = 0; i + k <= n; ++k) {
      cht.add(-a[i + k] * 2, dp[i + k][k] + a[i + k] * a[i + k]);
      dp[i + k + 1][k] = cht.monotone_inc_query(a[i + k]) + a[i + k] * a[i + k];
    }
  }
  FOR(j, 1, n + 1) cout << dp[n + 1][j] << '\n';
  return 0;
}
0