結果
問題 | No.2458 Line Up Charged Balls |
ユーザー | shobonvip |
提出日時 | 2023-09-01 23:28:38 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 54 ms / 2,000 ms |
コード長 | 3,604 bytes |
コンパイル時間 | 3,822 ms |
コンパイル使用メモリ | 265,192 KB |
実行使用メモリ | 8,064 KB |
最終ジャッジ日時 | 2024-06-11 05:38:13 |
合計ジャッジ時間 | 5,579 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 31 ms
8,064 KB |
testcase_06 | AC | 32 ms
7,984 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 31 ms
7,296 KB |
testcase_10 | AC | 10 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,944 KB |
testcase_13 | AC | 21 ms
6,944 KB |
testcase_14 | AC | 19 ms
6,940 KB |
testcase_15 | AC | 12 ms
6,940 KB |
testcase_16 | AC | 48 ms
7,988 KB |
testcase_17 | AC | 54 ms
8,064 KB |
testcase_18 | AC | 52 ms
7,916 KB |
testcase_19 | AC | 45 ms
8,064 KB |
testcase_20 | AC | 47 ms
7,936 KB |
testcase_21 | AC | 32 ms
7,968 KB |
testcase_22 | AC | 39 ms
8,060 KB |
testcase_23 | AC | 44 ms
7,936 KB |
testcase_24 | AC | 45 ms
8,064 KB |
testcase_25 | AC | 40 ms
7,936 KB |
testcase_26 | AC | 37 ms
8,064 KB |
testcase_27 | AC | 38 ms
8,020 KB |
ソースコード
#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'; }