結果

問題 No.703 ゴミ拾い Easy
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-25 10:39:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 168 ms / 1,500 ms
コード長 2,563 bytes
コンパイル時間 1,618 ms
コンパイル使用メモリ 134,920 KB
実行使用メモリ 9,484 KB
最終ジャッジ日時 2023-10-25 10:40:07
合計ジャッジ時間 8,367 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,024 KB
testcase_01 AC 2 ms
4,972 KB
testcase_02 AC 2 ms
5,028 KB
testcase_03 AC 2 ms
4,972 KB
testcase_04 AC 2 ms
4,972 KB
testcase_05 AC 2 ms
5,092 KB
testcase_06 AC 2 ms
4,972 KB
testcase_07 AC 2 ms
5,092 KB
testcase_08 AC 2 ms
5,028 KB
testcase_09 AC 2 ms
4,972 KB
testcase_10 AC 2 ms
5,028 KB
testcase_11 AC 2 ms
5,188 KB
testcase_12 AC 2 ms
4,972 KB
testcase_13 AC 2 ms
5,092 KB
testcase_14 AC 3 ms
4,972 KB
testcase_15 AC 3 ms
5,092 KB
testcase_16 AC 3 ms
5,092 KB
testcase_17 AC 3 ms
4,972 KB
testcase_18 AC 3 ms
5,092 KB
testcase_19 AC 3 ms
5,028 KB
testcase_20 AC 3 ms
4,972 KB
testcase_21 AC 3 ms
5,092 KB
testcase_22 AC 2 ms
5,092 KB
testcase_23 AC 3 ms
4,972 KB
testcase_24 AC 168 ms
9,480 KB
testcase_25 AC 145 ms
9,484 KB
testcase_26 AC 145 ms
9,484 KB
testcase_27 AC 145 ms
9,480 KB
testcase_28 AC 146 ms
9,484 KB
testcase_29 AC 145 ms
9,480 KB
testcase_30 AC 145 ms
9,480 KB
testcase_31 AC 148 ms
9,480 KB
testcase_32 AC 145 ms
9,480 KB
testcase_33 AC 145 ms
9,480 KB
testcase_34 AC 93 ms
9,432 KB
testcase_35 AC 92 ms
9,432 KB
testcase_36 AC 92 ms
9,432 KB
testcase_37 AC 93 ms
9,432 KB
testcase_38 AC 93 ms
9,432 KB
testcase_39 AC 94 ms
9,432 KB
testcase_40 AC 93 ms
9,432 KB
testcase_41 AC 93 ms
9,432 KB
testcase_42 AC 99 ms
9,432 KB
testcase_43 AC 92 ms
9,432 KB
testcase_44 AC 2 ms
5,028 KB
testcase_45 AC 2 ms
5,028 KB
testcase_46 AC 108 ms
9,428 KB
testcase_47 AC 107 ms
9,432 KB
testcase_48 AC 2 ms
5,028 KB
testcase_49 AC 2 ms
5,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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();
}
0