結果

問題 No.860 買い物
ユーザー BwambocosBwambocos
提出日時 2019-08-09 21:49:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,356 bytes
コンパイル時間 1,926 ms
コンパイル使用メモリ 170,896 KB
実行使用メモリ 24,700 KB
最終ジャッジ日時 2023-09-26 17:40:00
合計ジャッジ時間 5,852 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,760 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 TLE -
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

const LL inf = 1123456789012345;

// RMQ
class SegmentTree
{
private:
	LL n;
	std::vector<LL>node;

public:
	void init(LL size)
	{
		n = 1;
		while (n < size) n *= 2;
		node.resize(2 * n - 1, inf);
	}

	// node[i] += x 
	void update(LL i, LL x)
	{
		i += n - 1;
		node[i] = x;
		while (i > 0)
		{
			i = (i - 1) / 2;
			node[i] = std::min(node[2 * i + 1], node[2 * i + 2]);
		}
	}

	// min([a, b))
	// ex.) [0, N - 1] == [0, N)
	LL get_min(LL a, LL b, LL k = 0, LL l = 0, LL r = -1)
	{
		if (r < 0) r = n;
		if (r <= a || b <= l) return inf;
		if (a <= l && r <= b) return node[k];
		LL vl = get_min(a, b, 2 * k + 1, l, (l + r) / 2);
		LL vr = get_min(a, b, 2 * k + 2, (l + r) / 2, r);
		return std::min(vl, vr);
	}
};

LL N;
std::vector<LL>C, D, memo;
SegmentTree tree;

LL dp(LL i)
{
	if (i == N) return 0;
	if (memo[i] != -1) return memo[i];
	LL res = inf;
	for (LL j = i; j < N; ++j)
	{
		res = std::min(res, dp(j + 1) - D[i] + tree.get_min(i, j + 1));
	}
	return memo[i] = res;
}

int main()
{
	in >> N;
	C.resize(N); D.resize(N);
	rep(i, N) in >> C[i] >> D[i];


	LL ans = 0;
	rep(i, N) ans += C[i] + D[i];
	tree.init(N);
	rep(i, N) tree.update(i, C[i]);
	memo.resize(N, -1);
	ans += dp(0);
	out << ans << std::endl;
}
0