結果
問題 | No.2006 Decrease All to Zero |
ユーザー | 👑 hos.lyric |
提出日時 | 2022-07-09 00:06:44 |
言語 | D (dmd 2.106.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 7,068 bytes |
コンパイル時間 | 1,384 ms |
コンパイル使用メモリ | 123,232 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-22 15:41:54 |
合計ジャッジ時間 | 4,040 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | RE | - |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | WA | - |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | WA | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | AC | 1 ms
6,944 KB |
testcase_19 | AC | 1 ms
6,944 KB |
testcase_20 | WA | - |
testcase_21 | AC | 1 ms
6,940 KB |
testcase_22 | AC | 1 ms
6,944 KB |
testcase_23 | AC | 1 ms
6,940 KB |
testcase_24 | AC | 1 ms
6,940 KB |
testcase_25 | WA | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
ソースコード
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]); } } } 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(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))); } //*/ } // +L, +R { 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]); } } } */ foreach (l; 1 .. lim) { const t = ((l-1) & 1) ? 1 : 2; chmin(nxt[s + 2][l], rangeMin(s, t, max((l-1) - 2 * (A[i] - 2), 2 * (A[i] - 2) - (l-1)) + 1, (l-1) + 2 * (A[i] - 2) + 1)); } } 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) { } }