結果

問題 No.703 ゴミ拾い Easy
ユーザー ats5515ats5515
提出日時 2018-06-15 23:04:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 112 ms / 1,500 ms
コード長 2,978 bytes
コンパイル時間 1,174 ms
コンパイル使用メモリ 93,248 KB
実行使用メモリ 12,644 KB
最終ジャッジ日時 2023-08-30 18:56:11
合計ジャッジ時間 5,827 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 110 ms
12,492 KB
testcase_25 AC 112 ms
12,432 KB
testcase_26 AC 110 ms
12,384 KB
testcase_27 AC 111 ms
12,384 KB
testcase_28 AC 111 ms
12,392 KB
testcase_29 AC 111 ms
12,420 KB
testcase_30 AC 111 ms
12,456 KB
testcase_31 AC 112 ms
12,452 KB
testcase_32 AC 111 ms
12,644 KB
testcase_33 AC 110 ms
12,552 KB
testcase_34 AC 82 ms
12,420 KB
testcase_35 AC 82 ms
12,592 KB
testcase_36 AC 83 ms
12,548 KB
testcase_37 AC 83 ms
12,608 KB
testcase_38 AC 83 ms
12,620 KB
testcase_39 AC 82 ms
12,468 KB
testcase_40 AC 83 ms
12,496 KB
testcase_41 AC 84 ms
12,428 KB
testcase_42 AC 83 ms
12,492 KB
testcase_43 AC 83 ms
12,612 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 95 ms
12,472 KB
testcase_47 AC 94 ms
12,424 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <utility>
using namespace std;
#define int long long
int MOD = 1000000007;
template<typename T>
class ConvecHullTrick {
private:
	// 直線群(配列)
	std::vector<std::pair<T, T>> lines;
	// 最小値(最大値)を求めるxが単調であるか
	bool isMonotonicX;
	// 最小/最大を判断する関数
	std::function<bool(T l, T r)> comp;

public:
	// コンストラクタ ( クエリが単調であった場合はflag = trueとする )
	ConvecHullTrick(bool flagX = false, std::function<bool(T l, T r)> compFunc = [](T l, T r) {return l >= r; })
		:isMonotonicX(flagX), comp(compFunc) {
		//lines.emplace_back(0, 0);
	};

	// 直線l1, l2, l3のうちl2が不必要であるかどうか
	bool check(std::pair<T, T> l1, std::pair<T, T> l2, std::pair<T, T> l3) {
		if (l1 < l3) std::swap(l1, l3);
		return (l3.second - l2.second) * (l2.first - l1.first) >= (l2.second - l1.second) * (l3.first - l2.first);
	}

	// 直線y=ax+bを追加する
	void add(T a, T b) {
		std::pair<T, T> line(a, b);
		while (lines.size() >= 2 && check(*(lines.end() - 2), lines.back(), line))
			lines.pop_back();
		lines.emplace_back(line);
	}

	// i番目の直線f_i(x)に対するxの時の値を返す
	T f(int i, T x) {
		return lines[i].first * x + lines[i].second;
	}

	// i番目の直線f_i(x)に対するxの時の値を返す
	T f(std::pair<T, T> line, T x) {
		return line.first * x + line.second;
	}

	// 直線群の中でxの時に最小(最大)となる値を返す
	T get(T x) {
		// 最小値(最大値)クエリにおけるxが単調
		if (isMonotonicX) {
			static int head = 0;
			while (lines.size() - head >= 2 && comp(f(head, x), f(head + 1, x)))
				++head;
			return f(head, x);
		}
		else {
			int low = -1, high = lines.size() - 1;
			while (high - low > 1) {
				int mid = (high + low) / 2;
				(comp(f(mid, x), f(mid + 1, x)) ? low : high) = mid;
			}
			return f(high, x);
		}
	}
};
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<int> A(N);
	vector<int> X(N);
	vector<int> Y(N);
	vector<int> dp(N + 1, 0);
	int res = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		A[i]++;
	}
	for (int i = 0; i < N; i++) {
		cin >> X[i];
		X[i]++;
	}
	for (int i = 0; i < N; i++) {
		cin >> Y[i];
		//Y[i]++;
	}
	ConvecHullTrick<int> cht(false);
	//cht.add()
	cht.add(-2 * X[0], Y[0] * Y[0] + X[0] * X[0] + dp[0]);
	cerr << -2 * X[0] << endl;
	cerr << Y[0] * Y[0] + X[0] * X[0] + dp[0] << endl;
	///cht.get()
	cerr << cht.get(1) << endl;
	for (int i = 1; i <= N; i++) {
		int a = cht.get(A[i - 1]);
		dp[i] = a + A[i - 1] * A[i - 1];
		if (i < N)cht.add(-2 * X[i], Y[i] * Y[i] + X[i] * X[i] + dp[i]);
	}
	/*for (int i = 0; i <= N; i++) {
		cerr << dp[i] << " ";
	}
	cerr << endl;*/
	cout << dp[N] << endl;
}
0