結果
問題 | No.2006 Decrease All to Zero |
ユーザー | 👑 hos.lyric |
提出日時 | 2022-07-09 01:02:39 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 3,320 ms / 4,000 ms |
コード長 | 7,580 bytes |
コンパイル時間 | 3,673 ms |
コンパイル使用メモリ | 123,768 KB |
実行使用メモリ | 13,568 KB |
最終ジャッジ日時 | 2024-11-19 03:10:39 |
合計ジャッジ時間 | 32,880 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 139 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,820 KB |
testcase_04 | AC | 1 ms
6,816 KB |
testcase_05 | AC | 1 ms
6,820 KB |
testcase_06 | AC | 3 ms
6,820 KB |
testcase_07 | AC | 16 ms
6,820 KB |
testcase_08 | AC | 1 ms
6,820 KB |
testcase_09 | AC | 2 ms
6,816 KB |
testcase_10 | AC | 3,320 ms
13,504 KB |
testcase_11 | AC | 3,267 ms
13,544 KB |
testcase_12 | AC | 3,295 ms
13,568 KB |
testcase_13 | AC | 3,277 ms
13,524 KB |
testcase_14 | AC | 3,218 ms
13,460 KB |
testcase_15 | AC | 3,298 ms
13,416 KB |
testcase_16 | AC | 1,186 ms
13,480 KB |
testcase_17 | AC | 2,611 ms
13,524 KB |
testcase_18 | AC | 2 ms
6,816 KB |
testcase_19 | AC | 1 ms
6,816 KB |
testcase_20 | AC | 1 ms
6,816 KB |
testcase_21 | AC | 1 ms
6,816 KB |
testcase_22 | AC | 2 ms
6,816 KB |
testcase_23 | AC | 1 ms
6,820 KB |
testcase_24 | AC | 1 ms
6,820 KB |
testcase_25 | AC | 1 ms
6,816 KB |
testcase_26 | AC | 3,286 ms
13,496 KB |
testcase_27 | AC | 453 ms
13,536 KB |
testcase_28 | AC | 452 ms
13,544 KB |
testcase_29 | AC | 84 ms
6,820 KB |
ソースコード
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; 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]); } } } int segN = 1; for (; segN < lim; segN <<= 1) {} auto seg = new long[][][](3, 3, segN << 1); foreach (s; 0 .. 3) foreach (t; 1 .. 3) { seg[s][t][] = INF; } foreach (i; 1 .. N - 1) { foreach (s; 0 .. 3) foreach (t; 1 .. 3) { for (int k = t; k < lim; k += 2) { seg[s][t][segN + k] = crt[s][k]; } foreach_reverse (u; 1 .. segN) { seg[s][t][u] = min(seg[s][t][u << 1], seg[s][t][u << 1 | 1]); } } // a <= k <= b long rangeMin(int s, int t, int a, int b) { chmax(a, 0); chmin(++b, lim); long ret = INF; for (a += segN, b += segN; a < b; a >>= 1, b >>= 1) { if (a & 1) chmin(ret, seg[s][t][a++]); if (b & 1) chmin(ret, seg[s][t][--b]); } return ret; } auto nxt = new long[][](3, lim); foreach (s; 0 .. 3) { nxt[s][] = INF; } // foreach (s; 0 .. 3) { /* foreach (k; 1 .. lim) { const t = (k & 1) ? 1 : 2; for (int l = t; l < lim; l += 2) { if (abs(l - k) / 2 <= A[i] && A[i] <= t + (k - t) / 2 + (l - t) / 2) { chmin(nxt[s][l], crt[s][k]); } } } */ foreach (l; 1 .. lim) { const t = (l & 1) ? 1 : 2; chmin(nxt[s][l], rangeMin(s, t, max(l - 2 * A[i], 2 * A[i] - l), l + 2 * A[i])); } } // +L foreach (s; 0 .. 2) { /* foreach (k; 2 .. lim) { const t = ((k-1) & 1) ? 1 : 2; for (int l = t; l < lim; l += 2) { if (1 + abs(l - (k-1)) / 2 <= A[i] && A[i] <= 1 + t + ((k-1) - t) / 2 + (l - t) / 2) { chmin(nxt[s + 1][l], crt[s][k]); } } } //*/ //* foreach (l; 1 .. lim) { const t = (l & 1) ? 1 : 2; chmin(nxt[s + 1][l], rangeMin(s, t, max(1, l - 2 * (A[i] - 1), 2 * (A[i] - 1) - l) + 1, l + 2 * (A[i] - 1) + 1)); } //*/ } // +R foreach (s; 0 .. 2) { /* foreach (k; 1 .. lim) { const t = (k & 1) ? 1 : 2; for (int l = 1 + t; l < lim; l += 2) { if (1 + abs((l-1) - k) / 2 <= A[i] && A[i] <= 1 + t + (k - t) / 2 + ((l-1) - t) / 2) { chmin(nxt[s + 1][l], crt[s][k]); } } } //*/ //* foreach (l; 1 .. lim) { const t = ((l-1) & 1) ? 1 : 2; chmin(nxt[s + 1][l], rangeMin(s, t, max((l-1) - 2 * (A[i] - 1), 2 * (A[i] - 1) - (l-1)), (l-1) + 2 * (A[i] - 1))); } //*/ } // +LL, +LR, +RR { const s = 0; /* foreach (k; 1 .. lim) { const t = ((k-1) & 1) ? 1 : 2; for (int l = 1 + t; l < lim; l += 2) { if (2 + abs((l-1) - (k-1)) / 2 <= A[i] && A[i] <= 2 + t + ((k-1) - t) / 2 + ((l-1) - t) / 2) { chmin(nxt[s + 2][l], crt[s][k]); } } } //*/ //* // k-2, l-0 foreach (l; 1 .. lim) { const t = ((l-0) & 1) ? 1 : 2; chmin(nxt[s + 2][l], rangeMin(s, t, max((l-0) - 2 * (A[i] - 2) - 2, 2 * (A[i] - 2) - (l-0)) + 2, (l-0) + 2 * (A[i] - 2) + 2)); } // k-1, l-1 foreach (l; 2 .. lim) { const t = ((l-1) & 1) ? 1 : 2; chmin(nxt[s + 2][l], rangeMin(s, t, max((l-1) - 2 * (A[i] - 2) - 2, 2 * (A[i] - 2) - (l-1)) + 1, (l-1) + 2 * (A[i] - 2) + 1)); } // k-0, l-2 foreach (l; 3 .. lim) { const t = ((l-2) & 1) ? 1 : 2; chmin(nxt[s + 2][l], rangeMin(s, t, max((l-2) - 2 * (A[i] - 2) - 2, 2 * (A[i] - 2) - (l-2)) + 0, (l-2) + 2 * (A[i] - 2) + 0)); } //*/ } 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); assert(brt == ans); } } } } catch (EOFException e) { } }