結果

問題 No.2006 Decrease All to Zero
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-07-09 00:11:44
言語 D
(dmd 2.105.2)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 7,108 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 109,172 KB
実行使用メモリ 13,884 KB
最終ジャッジ日時 2023-09-04 17:45:17
合計ジャッジ時間 24,366 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 110 ms
6,688 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 3 ms
5,700 KB
testcase_07 AC 13 ms
5,360 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2,637 ms
13,680 KB
testcase_11 AC 2,623 ms
13,644 KB
testcase_12 AC 2,593 ms
13,884 KB
testcase_13 AC 2,606 ms
13,836 KB
testcase_14 AC 2,577 ms
13,832 KB
testcase_15 AC 2,616 ms
13,884 KB
testcase_16 AC 955 ms
13,640 KB
testcase_17 AC 2,055 ms
13,836 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2,584 ms
13,860 KB
testcase_27 AC 391 ms
13,880 KB
testcase_28 AC 391 ms
13,688 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)));
          }
          //*/
        }
        
        // +L, +R
        {
          const s = 0;
          if (maxA <= 100) {
          //*
          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]);
              }
            }
          }
          //*/
          } else {
          //*
          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) {
  }
}
0