結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,220 KB
testcase_01 AC 4 ms
8,176 KB
testcase_02 AC 4 ms
8,264 KB
testcase_03 AC 4 ms
8,224 KB
testcase_04 AC 4 ms
8,172 KB
testcase_05 AC 4 ms
8,236 KB
testcase_06 AC 4 ms
8,256 KB
testcase_07 AC 4 ms
8,316 KB
testcase_08 AC 4 ms
8,440 KB
testcase_09 AC 4 ms
8,248 KB
testcase_10 AC 3 ms
8,188 KB
testcase_11 AC 4 ms
8,220 KB
testcase_12 AC 4 ms
8,324 KB
testcase_13 AC 4 ms
8,236 KB
testcase_14 AC 4 ms
8,264 KB
testcase_15 AC 4 ms
8,216 KB
testcase_16 AC 4 ms
8,212 KB
testcase_17 AC 4 ms
8,180 KB
testcase_18 AC 4 ms
8,172 KB
testcase_19 AC 4 ms
8,188 KB
testcase_20 AC 4 ms
8,220 KB
testcase_21 AC 4 ms
8,264 KB
testcase_22 AC 4 ms
8,220 KB
testcase_23 AC 5 ms
8,324 KB
testcase_24 AC 255 ms
17,500 KB
testcase_25 AC 253 ms
17,492 KB
testcase_26 AC 252 ms
17,348 KB
testcase_27 AC 258 ms
17,360 KB
testcase_28 AC 254 ms
17,432 KB
testcase_29 AC 252 ms
17,440 KB
testcase_30 AC 252 ms
17,400 KB
testcase_31 AC 252 ms
17,612 KB
testcase_32 AC 253 ms
17,412 KB
testcase_33 AC 252 ms
17,488 KB
testcase_34 AC 212 ms
17,340 KB
testcase_35 AC 210 ms
17,396 KB
testcase_36 AC 214 ms
17,404 KB
testcase_37 AC 212 ms
17,340 KB
testcase_38 AC 213 ms
17,424 KB
testcase_39 AC 212 ms
17,340 KB
testcase_40 AC 215 ms
17,428 KB
testcase_41 AC 211 ms
17,400 KB
testcase_42 AC 214 ms
17,356 KB
testcase_43 AC 212 ms
17,500 KB
testcase_44 AC 4 ms
8,216 KB
testcase_45 AC 4 ms
8,436 KB
testcase_46 AC 280 ms
17,496 KB
testcase_47 AC 266 ms
17,416 KB
testcase_48 AC 5 ms
8,228 KB
testcase_49 AC 5 ms
8,400 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(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) : inf;
		while(k > 0)
		{
			k = (k-1)/2;
			if(usd[k])
			{
				ret = std::min(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