結果
| 問題 |
No.704 ゴミ拾い Medium
|
| コンテスト | |
| ユーザー |
miscalc
|
| 提出日時 | 2022-08-27 18:22:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 5,600 bytes |
| コンパイル時間 | 4,451 ms |
| コンパイル使用メモリ | 261,248 KB |
| 最終ジャッジ日時 | 2025-01-31 06:30:08 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 34 TLE * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tlll = tuple<ll, ll, ll>;
constexpr ll INF = 1LL << 60;
template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
ll safemod(ll A, ll M) {ll res = A % M; if (res < 0) res += M; return res;}
ll divfloor(ll A, ll B) {if (B < 0) {return divfloor(-A, -B);} return (A - safemod(A, B)) / B;}
ll divceil(ll A, ll B) {if (B < 0) {return divceil(-A, -B);} return divfloor(A + B - 1, B);}
ll pow_ll(ll A, ll B) {if (A == 0 || A == 1) {return A;} if (A == -1) {return B & 1 ? -1 : 1;} ll res = 1; for (int i = 0; i < B; i++) {res *= A;} return res;}
ll logfloor(ll A, ll B) {assert(A >= 2); ll res = 0; for (ll tmp = 1; tmp <= B / A; tmp *= A) {res++;} return res;}
ll logceil(ll A, ll B) {assert(A >= 2); ll res = 0; for (ll tmp = 1; tmp < B; tmp *= A) {res++;} return res;}
ll arisum_ll(ll a, ll d, ll n) { return n * a + (n & 1 ? ((n - 1) >> 1) * n : (n >> 1) * (n - 1)) * d; }
ll arisum2_ll(ll a, ll l, ll n) { return n & 1 ? ((a + l) >> 1) * n : (n >> 1) * (a + l); }
ll arisum3_ll(ll a, ll l, ll d) { assert((l - a) % d == 0); return arisum2_ll(a, l, (l - a) / d + 1); }
template<class T> void unique(vector<T> &V) {V.erase(unique(V.begin(), V.end()), V.end());}
template<class T> void sortunique(vector<T> &V) {sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end());}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)
template<class T> void printvec(const vector<T> &V) {int _n = V.size(); for (int i = 0; i < _n; i++) cout << V[i] << (i == _n - 1 ? "" : " ");cout << '\n';}
template<class T> void printvect(const vector<T> &V) {for (auto v : V) cout << v << '\n';}
template<class T> void printvec2(const vector<vector<T>> &V) {for (auto &v : V) printvec(v);}
//*
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint;
//*/
template<class T>
struct LiChaoTree
{
private:
int n;
vector<T> X;
vector<pair<T, T>> node;
int type;
void add_line_internal(T a, T b, int i, int l, int r)
{
auto [a0, b0] = node[i];
bool small_l = a * X[l] + b <= a0 * X[l] + b0;
bool small_r = a * X[r - 1] + b <= a0 * X[r - 1] + b0;
if (!small_l && !small_r)
return;
if (small_l && small_r)
{
node[i] = make_pair(a, b);
return;
}
if (i >= n)
return;
int m = (l + r) / 2;
bool small_m = a * X[m] + b <= a0 * X[m] + b0;
if (!small_m)
{
if (small_l)
add_line_internal(a, b, 2 * i, l, m);
else if (small_r)
add_line_internal(a, b, 2 * i + 1, m, r);
else
assert(false);
}
else
{
node[i] = make_pair(a, b);
if (!small_l)
add_line_internal(a0, b0, 2 * i, l, m);
else if (!small_r)
add_line_internal(a0, b0, 2 * i + 1, m, r);
else
assert(false);
}
}
void add_segment_internal(T a, T b, T lx, T rx, int i, int l, int r)
{
if (lx <= X[l] && X[r - 1] < rx)
{
add_line_internal(a, b, i, l, r);
return;
}
if (r - l <= 1)
return;
int m = (l + r) / 2;
add_segment_internal(a, b, lx, rx, 2 * i, l, m);
add_segment_internal(a, b, lx, rx, 2 * i + 1, m, r);
}
public:
LiChaoTree(const vector<T> &xs, bool sorted_unique = false) // クエリで問われる x の値を格納した配列を渡す
{
type = 0;
X = vector<T>(xs);
if (!sorted_unique)
sortunique(X);
n = 1;
while (n < (int)X.size())
n <<= 1;
X.resize(2 * n, X.back());
node.assign(2 * n, make_pair(0, INF));
}
LiChaoTree(T x_min, T x_max) // 上の xs = {x_min, …, x_max} と同じ(get_min が少し早くなる)
{
type = 1;
X.resize(x_max - x_min + 1);
for (int i = 0; i <= x_max - x_min; i++)
X[i] = x_min + i;
n = 1;
while (n < (int)X.size())
n <<= 1;
X.resize(2 * n, X.back());
node.assign(2 * n, make_pair(0, INF));
}
void add_line(T a, T b) { add_line_internal(a, b, 1, 0, n); }
void add_segment(T a, T b, T lx, T rx) { add_segment_internal(a, b, lx, rx, 1, 0, n); }
T get_min(T p)
{
int i;
if (type == 0)
{
i = lower_bound(X.begin(), X.end(), p) - X.begin();
assert(X.at(i) == p);
}
else if (type == 1)
{
assert(X.front() <= p && p <= X.back());
i = p - X.front();
}
else
assert(false);
i += n;
T res = INF;
while (i > 0)
{
auto [a, b] = node[i];
chmin(res, a * p + b);
i >>= 1;
}
return res;
}
};
int main()
{
ll N;
cin >> N;
vector<ll> A(N), X(N), Y(N);
for (ll i = 0; i < N; i++)
{
cin >> A.at(i);
}
for (ll i = 0; i < N; i++)
{
cin >> X.at(i);
}
for (ll i = 0; i < N; i++)
{
cin >> Y.at(i);
}
LiChaoTree<ll> cht(A);
/*
vector<ll> dp(N + 1, INF);
dp.at(0) = 0;
for (ll i = 0; i < N; i++)
{
for (ll j = 0; j <= i; j++)
{
ll tmp = dp.at(j) + abs(X.at(j) - A.at(i)) + Y.at(j);
chmin(dp.at(i + 1), tmp);
}
}
ll ans = dp.at(N);
cout << ans << endl;
//*/
//*
vector<ll> dp(N + 1, INF);
dp.at(0) = 0;
for (ll i = 0; i < N; i++)
{
cht.add_segment(-1, X.at(i) + Y.at(i) + dp.at(i), -INF, X.at(i));
cht.add_segment(1, -X.at(i) + Y.at(i) + dp.at(i), X.at(i), INF);
dp.at(i + 1) = cht.get_min(A.at(i));
}
ll ans = dp.at(N);
cout << ans << endl;
//*/
}
miscalc