結果
| 問題 |
No.2458 Line Up Charged Balls
|
| コンテスト | |
| ユーザー |
sotanishy
|
| 提出日時 | 2023-09-01 23:25:48 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 76 ms / 2,000 ms |
| コード長 | 2,302 bytes |
| コンパイル時間 | 2,370 ms |
| コンパイル使用メモリ | 200,440 KB |
| 最終ジャッジ日時 | 2025-02-16 17:26:34 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
return a > b ? (a = b, 1) : 0;
}
template <typename T>
class ConvexHullTrick {
public:
void add(T a, T b) {
a = -a, b = -b;
auto m = lines.insert({a, b, 0});
auto l = m, r = m;
++r;
while (update(m, r)) {
r = lines.erase(r);
}
if (l != lines.begin() && update(--l, m)) {
m = lines.erase(m);
update(l, m);
}
m = l;
while (l != lines.begin() && (--l)->p >= m->p) {
update(l, lines.erase(m));
m = l;
}
}
T get(T x) const {
assert(!lines.empty());
auto it = *lines.lower_bound(x);
return -(it.a * x + it.b);
}
private:
struct Line {
mutable T a, b; // ax + b
mutable double p; // intersection point with the next line
bool operator<(const Line& o) const { return a < o.a; }
bool operator<(T x) const { return p < x; }
};
using iterator = typename std::multiset<Line, std::less<>>::iterator;
static constexpr double INF = std::numeric_limits<double>::max() / 2;
std::multiset<Line, std::less<>> lines;
bool update(iterator x, iterator y) const {
if (y == lines.end()) {
x->p = INF;
return false;
}
if (x->a == y->a) {
x->p = (x->b > y->b ? INF : -INF);
} else {
x->p = 1.0 * (y->b - x->b) / (x->a - y->a);
}
return x->p >= y->p;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int N;
cin >> N;
vector<ll> Q(N);
for (auto& x : Q) cin >> x;
vector<ll> dp(N);
ConvexHullTrick<ll> cht;
cht.add(-Q[0], 0);
ll ans = 0;
rep(i, 1, N) {
dp[i] = -cht.get(Q[i]);
cht.add(-Q[i], -max(0LL, dp[i]));
chmax(ans, dp[i]);
}
cout << ans << endl;
}
sotanishy