結果
| 問題 |
No.860 買い物
|
| コンテスト | |
| ユーザー |
Bwambocos
|
| 提出日時 | 2019-08-09 21:49:57 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,356 bytes |
| コンパイル時間 | 1,685 ms |
| コンパイル使用メモリ | 171,860 KB |
| 実行使用メモリ | 10,624 KB |
| 最終ジャッジ日時 | 2024-07-19 11:36:49 |
| 合計ジャッジ時間 | 5,519 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 3 TLE * 2 -- * 10 |
ソースコード
#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;
}
Bwambocos