結果
| 問題 |
No.703 ゴミ拾い Easy
|
| コンテスト | |
| ユーザー |
ゆにぽけ
|
| 提出日時 | 2023-10-25 10:39:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 206 ms / 1,500 ms |
| コード長 | 2,563 bytes |
| コンパイル時間 | 1,498 ms |
| コンパイル使用メモリ | 130,356 KB |
| 最終ジャッジ日時 | 2025-02-17 13:33:07 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 46 |
ソースコード
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
#include<functional>
#include<utility>
using namespace std;
template<class T = long long> struct ConvexHullTrick
{
private:
bool minflag;
static const T Q = numeric_limits<T>::lowest();
struct Line
{
T a,b;
mutable function<const Line*()> NEX;
bool operator<(const Line &other) const
{
if(b == Q)
{
const Line* nl = NEX();
if(!nl) return true;
return (nl -> a - other.a) * a + (nl -> b - other.b) < 0;
}
else if(other.b == Q)
{
const Line* nl = NEX();
if(!nl) return false;
return (nl -> a - a) * other.a + (nl -> b - b) > 0;
}
else return a < other.a;
}
};
bool isNotNeeded(const typename set<Line>::iterator it)
{
const auto nt = next(it);
if(it == Lines.begin())
{
if(nt == Lines.end()) return false;
return (*it).a == (*nt).a && (*it).b <= (*nt).b;
}
else
{
const auto pt = prev(it);
if(nt == Lines.end()) return (*it).a == (*pt).a && (*it).b <= (*pt).b;
else return ((*pt).b-(*it).b) * ((*nt).a-(*it).a) >= ((*nt).b-(*it).b) * ((*pt).a-(*it).a);
}
}
multiset<Line> Lines;
public:
constexpr ConvexHullTrick(bool minflag = true) : minflag(minflag) {}
constexpr void add(T a,T b) noexcept
{
if(minflag) a = -a,b = -b;
auto it = Lines.insert(Line{a,b});
(*it).NEX = [=] {return next(it) == Lines.end() ? nullptr : &*next(it);};
if(isNotNeeded(it)) Lines.erase(it);
else
{
while(next(it) != Lines.end() && isNotNeeded(next(it))) Lines.erase(next(it));
while(it != Lines.begin() && isNotNeeded(prev(it))) Lines.erase(prev(it));
}
}
constexpr T get(T x) noexcept
{
Line l = *Lines.lower_bound(Line{x,Q});
T res = l.a * x + l.b;
if(minflag) res = -res;
return res;
}
};
int N,A[3 << 17],X[3 << 17],Y[3 << 17];
long long dp[3 << 17];
void solve()
{
//dp[i] = min{dp[j] + dist};
cin >> N;
for(int i = 0;i < N;i++) cin >> A[i];
for(int i = 0;i < N;i++) cin >> X[i];
for(int i = 0;i < N;i++) cin >> Y[i];
ConvexHullTrick CHT;
for(int i = 1;i <= N;i++)
{
CHT.add(-2*X[i-1],(long long)X[i-1]*X[i-1]+(long long)Y[i-1]*Y[i-1]+dp[i-1]);
dp[i] = CHT.get(A[i-1])+(long long)A[i-1]*A[i-1];
}
cout << dp[N] << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
//cin >> tt;
while(tt--) solve();
}
ゆにぽけ