結果
| 問題 |
No.2006 Decrease All to Zero
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-07-08 23:43:01 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 5,232 bytes |
| コンパイル時間 | 1,043 ms |
| コンパイル使用メモリ | 122,496 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-22 15:40:42 |
| 合計ジャッジ時間 | 3,613 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 RE * 1 |
| other | AC * 13 RE * 14 |
ソースコード
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)); }
enum INF = 10L^^18;
void main() {
try {
for (; ; ) {
const N = readInt;
auto X = new long[N];
foreach (i; 0 .. N) {
X[i] = readLong;
}
auto A = new int[N];
foreach (i; 0 .. N) {
A[i] = readInt;
}
if (N == 1) {
writeln((A[0] == 1) ? 0 : -1);
continue;
}
const maxA = A.maxElement;
if (maxA >= 100) {
assert(false);
}
const lim = 2 * maxA + 5;
auto crt = new long[][](3, lim);
foreach (s; 0 .. 3) {
crt[s][] = INF;
}
// i = 0
foreach (s; 0 .. 3) {
for (int k = s; k < lim; k += 2) if (s + (k - s) / 2 == A[0]) {
crt[s][k] = k * (X[1] - X[0]);
}
}
debug {
if (lim <= 20) {
foreach (s; 0 .. 3) {
writefln("%s %s: %s", 0, s, crt[s]);
}
}
}
foreach (i; 1 .. N - 1) {
auto nxt = new long[][](3, lim);
foreach (s; 0 .. 3) {
nxt[s][] = INF;
}
// TODO: speed up
//
foreach (s; 0 .. 3) {
foreach (k; 1 .. lim) {
const kk = (k & 1) ? 1 : 2;
for (int l = kk; l < lim; l += 2) {
if (abs(l - k) / 2 <= A[i] && A[i] <= kk + (k - kk) / 2 + (l - kk) / 2) {
chmin(nxt[s][l], crt[s][k]);
}
}
}
}
// +L
foreach (s; 0 .. 2) {
foreach (k; 2 .. lim) {
const kk = ((k-1) & 1) ? 1 : 2;
for (int l = kk; l < lim; l += 2) {
if (1 + abs(l - (k-1)) / 2 <= A[i] && A[i] <= 1 + kk + ((k-1) - kk) / 2 + (l - kk) / 2) {
chmin(nxt[s + 1][l], crt[s][k]);
}
}
}
}
// +R
foreach (s; 0 .. 2) {
foreach (k; 1 .. lim) {
const kk = (k & 1) ? 1 : 2;
for (int l = 1 + kk; l < lim; l += 2) {
if (1 + abs((l-1) - k) / 2 <= A[i] && A[i] <= 1 + kk + (k - kk) / 2 + ((l-1) - kk) / 2) {
chmin(nxt[s + 1][l], crt[s][k]);
}
}
}
}
// +L, +R
{
const s = 0;
foreach (k; 1 .. lim) {
const kk = ((k-1) & 1) ? 1 : 2;
for (int l = 1 + kk; l < lim; l += 2) {
if (2 + abs((l-1) - (k-1)) / 2 <= A[i] && A[i] <= 2 + kk + ((k-1) - kk) / 2 + ((l-1) - kk) / 2) {
chmin(nxt[s + 2][l], crt[s][k]);
}
}
}
}
crt = nxt;
foreach (s; 0 .. 3) foreach (k; 1 .. lim) {
crt[s][k] += k * (X[i + 1] - X[i]);
}
debug {
if (lim <= 20) {
foreach (s; 0 .. 3) {
writefln("%s %s: %s", i, s, crt[s]);
}
}
}
}
// i = N - 1
long ans = INF;
foreach (s; 0 .. 3) {
for (int k = (2-s); k < lim; k += 2) if ((2-s) + (k - (2-s)) / 2 == A[N - 1]) {
chmin(ans, crt[s][k]);
debug {
if (lim <= 20) {
writefln("%s %s %s: %s", N - 1, s, k, crt[s][k]);
}
}
}
}
writeln((ans >= INF) ? -1 : ans);
debug {
if (A.sum <= 10) {
int[] ps;
foreach (i; 0 .. N) {
foreach (_; 0 .. A[i]) {
ps ~= i;
}
}
long brt = INF;
int[] psm;
do {
bool ok = true;
long cost;
foreach (j; 0 .. cast(int)(ps.length) - 1) {
ok = ok && (ps[j] != ps[j + 1]);
cost += abs(X[ps[j + 1]] - X[ps[j]]);
}
if (ok) {
if (chmin(brt, cost)) {
psm = ps.dup;
}
}
} while (ps.nextPermutation);
writeln("brt = ", brt, "; ", psm);
}
}
}
} catch (EOFException e) {
}
}