結果
| 問題 |
No.952 危険な火薬庫
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-12-15 00:19:37 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 260 ms / 2,000 ms |
| コード長 | 3,485 bytes |
| コンパイル時間 | 924 ms |
| コンパイル使用メモリ | 124,000 KB |
| 実行使用メモリ | 157,256 KB |
| 最終ジャッジ日時 | 2024-06-22 03:37:42 |
| 合計ジャッジ時間 | 4,031 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 23 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
// floor(a / b)
Int divFloor(Int)(Int a, Int b) {
return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);
}
// ceil(a / b)
Int divCeil(Int)(Int a, Int b) {
return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);
}
enum INF = 10L^^18;
// a/b vs c/d
int cmp(long a, long b, long c, long d) {
const x = divFloor(a, b);
const y = divFloor(c, d);
if (x != y) return (x < y) ? -1 : (x > y) ? +1 : 0;
a -= b * x;
c -= d * y;
if (b < 0) { a *= -1; b *= -1; }
if (d < 0) { c *= -1; d *= -1; }
return (a * d < c * b) ? -1 : (a * d > c * b) ? +1 : 0;
}
void main() {
try {
for (; ; ) {
const N = readInt();
auto A = new long[N + 2];
foreach (i; 1 .. N + 1) {
A[i] = readLong();
}
auto S = new long[N + 3];
foreach (i; 0 .. N + 2) {
S[i + 1] = S[i] + A[i];
}
auto dp = new long[][](N + 1, N + 2);
foreach (k; 0 .. N + 1) {
dp[k][] = INF;
}
dp[0][0] = 0;
foreach (k; 1 .. N + 1) {
/*
dp[k][i] = \min_{0<=j<i} (dp[k - 1][j] + (S[i] - S[j + 1])^2)
= S[i]^^2 + \min_{0<=j<i} (dp[k - 1][j] + S[j + 1]^2 - 2 S[j + 1] S[i])
(- 2 S[j + 1]): decr
*/
alias Entry = Tuple!(long, long);
long eval(ref Entry e, long x) {
return e[0] + e[1] * x;
}
auto que = new Entry[N + 2];
int head, tail;
foreach (i; 1 .. N + 2) {
// push i - 1
if (dp[k - 1][i - 1] < INF) {
const a0 = dp[k - 1][i - 1] + S[i]^^2;
const a1 = -2 * S[i];
for (; tail - head >= 2 &&
cmp(-(que[tail - 1][0] - que[tail - 2][0]), que[tail - 1][1] - que[tail - 2][1],
-(a0 - que[tail - 2][0]), a1 - que[tail - 2][1]) >= 0; --tail) {}
que[tail++] = Entry(a0, a1);
}
for (; tail - head >= 2 &&
eval(que[head], S[i]) >= eval(que[head + 1], S[i]); ++head) {}
if (tail - head >= 1) {
dp[k][i] = S[i]^^2 + eval(que[head], S[i]);
}
}
}
debug {
foreach (k; 0 .. N + 1) {
writeln(dp[k]);
}
}
foreach_reverse (k; 1 .. N + 1) {
writeln(dp[k][N + 1]);
}
}
} catch (EOFException e) {
}
}