結果
| 問題 |
No.703 ゴミ拾い Easy
|
| コンテスト | |
| ユーザー |
oshibori
|
| 提出日時 | 2018-06-16 19:36:37 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 130 ms / 1,500 ms |
| コード長 | 2,615 bytes |
| コンパイル時間 | 1,819 ms |
| コンパイル使用メモリ | 170,228 KB |
| 実行使用メモリ | 12,800 KB |
| 最終ジャッジ日時 | 2025-01-02 13:57:21 |
| 合計ジャッジ時間 | 6,658 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 46 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
#define int long long
// typedef __int128_t Int;
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// source http://satanic0258.hatenablog.com/entry/2016/08/16/181331
// https://yukicoder.me/problems/no/703
class ConvexHullTrick {
public:
deque<pair<int, int>> lines;
bool isMonotonic = false;
function<bool(int, int)> comp = [](int l, int r) {
return l >= r; // min
// return l <= r; // max
};
bool check(pair<int, int> l1, pair<int, int> l2, pair<int, int> l3) {
int a1 = l1.first, b1 = l1.second;
int a2 = l2.first, b2 = l2.second;
int a3 = l3.first, b3 = l3.second;
return (a2 - a3) * (b1 - b2) <= (a1 - a2) * (b2 - b3);
}
void add(int a, int b) {
pair<int, int> line(a, b);
while (lines.size() >= 2 and
check(lines[lines.size() - 2], lines[lines.size() - 1], line)) {
lines.pop_back();
}
lines.emplace_back(line);
}
int f(int j, int x) { return lines[j].first * x + lines[j].second; }
int query(int x) {
if (isMonotonic) {
while (lines.size() >= 2 and comp(f(0, x), f(1, x))) {
lines.pop_front();
}
return f(0, x);
} else {
int l = 0, r = lines.size();
while (r - l > 1) {
int m = (l + r) / 2;
if (comp(f(m, x), f(m - 1, x))) {
r = m;
} else {
l = m;
}
}
return f(l, x);
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int N;
cin >> N;
vector<int> a(N);
rep(i, 0, N) { cin >> a[i]; }
vector<int> x(N);
rep(i, 0, N) { cin >> x[i]; }
vector<int> y(N);
rep(i, 0, N) { cin >> y[i]; }
ConvexHullTrick cht;
vector<int> dp(N + 1);
dp[0] = 0;
rep(i, 1, N + 1) {
cht.add(-2 * x[i - 1],
dp[i - 1] + x[i - 1] * x[i - 1] + y[i - 1] * y[i - 1]);
dp[i] = a[i - 1] * a[i - 1] + cht.query(a[i - 1]);
}
cout << dp[N] << endl;
return 0;
}
oshibori