結果
| 問題 |
No.703 ゴミ拾い Easy
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-07-16 04:38:57 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 99 ms / 1,500 ms |
| コード長 | 2,735 bytes |
| コンパイル時間 | 1,807 ms |
| コンパイル使用メモリ | 166,772 KB |
| 実行使用メモリ | 12,672 KB |
| 最終ジャッジ日時 | 2025-01-02 13:57:32 |
| 合計ジャッジ時間 | 6,290 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 46 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template<class Numeric> struct ConvexHullTrick {
struct Line { // f: y=ax+b
Numeric a; Numeric b;
Line(Numeric _a, Numeric _b) : a(_a), b(_b) {}
bool operator< (Line &l) {
if (a != l.a) return a < l.a;
else return b < l.b;
}
};
private:
vector<Line> lines;
bool isMinimumQuery;
bool isMonotonicQuery;
bool compare(Numeric a, Numeric b, bool equal = true) {
if (equal) {
if (isMinimumQuery) return b <= a;
else return a <= b;
} else {
if (isMinimumQuery) return b < a;
else return a < b;
}
}
public:
// constructor
ConvexHullTrick(bool monotonic, bool minimum = true) :
isMinimumQuery(minimum), isMonotonicQuery(monotonic) {}
// check the necessity of line2
bool check(Line l1, Line l2, Line l3) {
if (l1 < l3) swap(l3, l1);
return compare((l3.b-l2.b)*(l2.a-l1.a), (l2.b-l1.b)*(l3.a-l2.a));
}
// add the line: y=ax+b
void add(Numeric a, Numeric b) {
Line line(a, b);
int size = (int) lines.size();
if (size >= 1 && lines[size-1].a == line.a){
if (!compare(lines[size-1].b, line.b)) return;
}
while (size >= 2 && check(lines[size-2], lines[size-1], line)){
lines.pop_back(); --size;
}
lines.emplace_back(line);
}
// get the value of line(x)
Numeric f(Line line, Numeric x) {
return line.a * x + line.b;
}
// solve
Numeric solve(Numeric x) {
if (isMonotonicQuery) {
static int head = 0;
if ((int) lines.size() <= head) head = lines.size()-1;
while (head > 0 && compare(f(lines[head], x), f(lines[head-1], x), false)) --head;
while (lines.size() - head >= 2 && compare(f(lines[head], x), f(lines[head+1], x))) ++head;
return f(lines[head], x);
}
else {
int low = -1; int high = lines.size()-1;
while (high - low > 1) {
int mid = (high+low)/2;
if (compare(f(lines[mid], x), f(lines[mid+1], x))) low = mid;
else high = mid;
}
return f(lines[high], x);
}
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<long long> a(n), x(n), y(n);
for (int i = 0; i < n; i++) {
cin >> a[n-i-1];
}
for (int i = 0; i < n; i++) {
cin >> x[n-i-1];
}
for (int i = 0; i < n; i++) {
cin >> y[n-i-1];
}
ConvexHullTrick<long long> convexhulltrick(true);
vector<long long> dp(n+1, 0);
for (int i = 0; i < n; i++) {
convexhulltrick.add(2*a[i], dp[i]+a[i]*a[i]);
dp[i+1] = convexhulltrick.solve(-x[i]) + x[i]*x[i] + y[i]*y[i];
}
cout << dp[n] << endl;
return 0;
}