結果

問題 No.703 ゴミ拾い Easy
ユーザー zaki_johozaki_joho
提出日時 2019-10-24 01:19:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 276 ms / 1,500 ms
コード長 2,423 bytes
コンパイル時間 1,767 ms
コンパイル使用メモリ 176,700 KB
実行使用メモリ 17,620 KB
最終ジャッジ日時 2023-08-30 19:12:38
合計ジャッジ時間 9,232 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,324 KB
testcase_01 AC 4 ms
8,216 KB
testcase_02 AC 3 ms
8,244 KB
testcase_03 AC 4 ms
8,244 KB
testcase_04 AC 4 ms
8,216 KB
testcase_05 AC 5 ms
8,252 KB
testcase_06 AC 5 ms
8,264 KB
testcase_07 AC 4 ms
8,232 KB
testcase_08 AC 4 ms
8,292 KB
testcase_09 AC 4 ms
8,256 KB
testcase_10 AC 4 ms
8,268 KB
testcase_11 AC 3 ms
8,436 KB
testcase_12 AC 4 ms
8,248 KB
testcase_13 AC 4 ms
8,260 KB
testcase_14 AC 4 ms
8,168 KB
testcase_15 AC 5 ms
8,440 KB
testcase_16 AC 5 ms
8,264 KB
testcase_17 AC 5 ms
8,260 KB
testcase_18 AC 5 ms
8,264 KB
testcase_19 AC 4 ms
8,328 KB
testcase_20 AC 5 ms
8,280 KB
testcase_21 AC 4 ms
8,172 KB
testcase_22 AC 5 ms
8,220 KB
testcase_23 AC 5 ms
8,168 KB
testcase_24 AC 249 ms
17,488 KB
testcase_25 AC 252 ms
17,488 KB
testcase_26 AC 250 ms
17,484 KB
testcase_27 AC 252 ms
17,412 KB
testcase_28 AC 251 ms
17,428 KB
testcase_29 AC 250 ms
17,620 KB
testcase_30 AC 250 ms
17,400 KB
testcase_31 AC 251 ms
17,360 KB
testcase_32 AC 251 ms
17,496 KB
testcase_33 AC 252 ms
17,428 KB
testcase_34 AC 210 ms
17,472 KB
testcase_35 AC 208 ms
17,396 KB
testcase_36 AC 211 ms
17,428 KB
testcase_37 AC 208 ms
17,388 KB
testcase_38 AC 211 ms
17,464 KB
testcase_39 AC 211 ms
17,408 KB
testcase_40 AC 212 ms
17,432 KB
testcase_41 AC 209 ms
17,340 KB
testcase_42 AC 209 ms
17,424 KB
testcase_43 AC 208 ms
17,408 KB
testcase_44 AC 4 ms
8,220 KB
testcase_45 AC 4 ms
8,264 KB
testcase_46 AC 276 ms
17,476 KB
testcase_47 AC 261 ms
17,392 KB
testcase_48 AC 5 ms
8,260 KB
testcase_49 AC 5 ms
8,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

class LiChaoTree
{
	using T = long long;
	using Line = std::pair<T, T>;
	const T inf = 1e15;
	// 最小化?
	bool isMinimum;
	int n;
	// 各区間には, その区間で最小(or最大)を取りうる直線を保持する
	std::vector<Line> node;
	std::vector<int> usd;

	static inline T f(const Line &line, T x) { return line.first * x + line.second; }

	// [l, r) に Line を追加, 現在ノードk
	void add(int k, int l, int r, Line line)
	{
		while (r - l > 0)
		{
			if (!usd[k])
			{
				node[k] = line;
				usd[k] = true;
				return;
			}
			int m = (l + r) / 2;
			bool left = (f(line, l) < f(node[k], l));
			bool mid = (f(line, m) < f(node[k], m));
			bool right = (f(line, r) < f(node[k], r));
			if (!isMinimum)
			{
				left = !left;
				mid = !mid;
				right = !right;
			}
			if (left && right)
			{
				node[k] = line;
				return;
			}
			if (!left && !right)
			{
				return;
			}
			if (mid)
			{
				swap(line, node[k]);
			}
			if (left != mid)
			{
				k = 2 * k + 1;
				r = m;
			}
			else
			{
				k = 2 * k + 2;
				l = m;
			}
		}
	}

public:
	// [0, n] 用の木を作る
	LiChaoTree(int _n, bool isMin = true) : isMinimum(isMin)
	{
		n = 1;
		while (n < _n)
			n *= 2;
		node.resize(2 * n - 1, Line(0, isMin ? inf : -inf));
		usd.resize(2 * n - 1, false);
	}
	// ax + b を追加する
	void add(T a, T b)
	{
		add(0, 0, n, Line(a, b));
		return;
	}
	T get(T x)
	{
		int k = x + (n - 1);
		T ret = usd[k] ? f(node[k], x) : (isMinimum ? inf : -inf);
		while (k > 0)
		{
			k = (k - 1) / 2;
			if (usd[k])
			{
				ret = isMinimum ? std::min(ret, f(node[k], x)) : std::max(ret, f(node[k], x));
			}
		}
		return ret;
	}
};


// https://yukicoder.me/submissions/392545
void solve_yukicoder_703()
{
    using ll = long long;
    const ll inf = 1e15;
    int n;
    cin >> n;
    vector<ll> a(n+1), x(n+1), y(n+1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> x[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> y[i];
    }
    LiChaoTree cht(100010);
    vector<ll> dp(n + 1, inf);
    dp[0] = 0;
    for (int i = 1; i <= n; i++)
    {
        cht.add(-2 * x[i], x[i] * x[i] + y[i] * y[i] + dp[i-1]);
        dp[i] = cht.get(a[i]) + a[i] * a[i];
        //cerr << dp[i] << endl;
    }
    cout << dp[n] << endl;
}

int main()
{
    solve_yukicoder_703();
}
0