結果

問題 No.2458 Line Up Charged Balls
ユーザー shobonvip
提出日時 2023-09-01 23:28:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 3,604 bytes
コンパイル時間 4,285 ms
コンパイル使用メモリ 253,876 KB
最終ジャッジ日時 2025-02-16 17:28:50
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}


// https://tjkendev.github.io/procon-library/cpp/convex_hull_trick/li_chao_tree_dynamic.html
class LiChaoTree {
  struct Line {
    ll a, b;
    ll f(ll x) const { return a*x + b; }
    Line(ll a, ll b) : a(a), b(b) {}
  };
  struct Node {
    Node *left, *right;
    Line line;

    Node(Line line) : left(nullptr), right(nullptr), line(line) {}
  };
  const ll inf = 1e16L;
  const Line inf_line = Line{0, inf};

  Node* root;
  ll xl, xr;

  Node* _add_line(Node *nd, Line line, ll l, ll r) {
    if(l == r) return nullptr;

    ll m = (l + r) >> 1;
    if(nd == nullptr) return new Node(line);

    bool left = (line.f(l) <= nd->line.f(l));
    bool mid = (line.f(m) <= nd->line.f(m));
    bool right = (line.f(r) <= nd->line.f(r));
    if(left && right) {
      nd->line = line;
      return nd;
    }
    if(!left && !right) {
      return nd;
    }
    if(mid) {
      swap(nd->line, line);
    }
    if(left != mid) {
      nd->left = _add_line(nd->left, line, l, m);
    } else {
      nd->right = _add_line(nd->right, line, m, r);
    }
    return nd;
  }

  Node* _add_segment_line(ll a, ll b, Node *nd, Line line, ll l, ll r) {
    if(r <= a || b <= l) {
      return nd;
    }
    if(a <= l && r <= b) {
      return _add_line(nd, line, l, r);
    }

    if(nd == nullptr) {
      nd = new Node(inf_line);
    }

    ll m = (l + r) >> 1;
    nd->left = _add_segment_line(a, b, nd->left, line, l, m);
    nd->right = _add_segment_line(a, b, nd->right, line, m, r);
    return nd;
  }

  ll _query(ll k, ll l, ll r) {
    Node *nd = root;
    ll s = inf;
    while(r-l > 0 && nd != nullptr) {
      ll m = (l + r) >> 1;
      s = min(s, nd->line.f(k));
      if(k < m) {
        r = m;
        nd = nd->left;
      } else {
        l = m;
        nd = nd->right;
      }
    }
    return s;
  }

public:
  // [xl, xr)
  LiChaoTree(ll xl, ll xr) : xl(xl), xr(xr), root(nullptr) {}

  void add_line(ll a, ll b) {
    Line line = Line{a, b};
    root = _add_line(root, line, xl, xr);
  }

  void add_segment_line(ll a, ll b, ll l, ll r) {
    Line line = Line{a, b};
    root = _add_segment_line(l, r, root, line, xl, xr);
  }

  ll query(ll x) {
    return _query(x, xl, xr);
  }
};

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n; cin >> n;
	vector<ll> a(n);
	rep(i,0,n) cin >> a[i];
	LiChaoTree lc(-1e5, 1e5);
	vector<ll> dp(n+1);
	dp[0] = 0;
	lc.add_line(-0, -0);
	rep(i,0,n){
		ll v = lc.query(a[i]);
		dp[i+1] = -v;
		lc.add_line(-a[i], -dp[i+1]);
	}
	ll ans = 0;
	rep(i,0,n+1){
		chmax(ans, dp[i]);
	}
	cout << ans << '\n';
}

0