結果

問題 No.1297 銅像
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-11-24 12:22:38
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 5,929 bytes
コンパイル時間 1,375 ms
コンパイル使用メモリ 107,932 KB
実行使用メモリ 23,512 KB
最終ジャッジ日時 2023-09-04 11:11:21
合計ジャッジ時間 6,629 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 10 ms
4,380 KB
testcase_07 AC 10 ms
4,380 KB
testcase_08 AC 10 ms
4,380 KB
testcase_09 AC 199 ms
22,876 KB
testcase_10 AC 188 ms
23,512 KB
testcase_11 AC 168 ms
14,496 KB
testcase_12 AC 167 ms
15,740 KB
testcase_13 AC 170 ms
21,096 KB
testcase_14 AC 145 ms
13,700 KB
testcase_15 AC 144 ms
13,648 KB
testcase_16 AC 142 ms
13,412 KB
testcase_17 AC 152 ms
14,176 KB
testcase_18 AC 165 ms
15,296 KB
testcase_19 AC 172 ms
21,940 KB
testcase_20 AC 180 ms
14,764 KB
testcase_21 AC 196 ms
16,732 KB
testcase_22 AC 176 ms
15,152 KB
権限があれば一括ダウンロードができます

ソースコード

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)); }


// class Func = TX -> TY
//   for any f, g: Func, x -> sgn(g(x) - f(x)) must be monotone
class LiChaoTree(Func) {
  import std.algorithm : swap;
  alias TX = Func.TX;
  alias TY = Func.TY;
  const(TX) L, R;
  class Tree {
    Tree l, r;
    Func f;
    this(Func f) {
      this.f = f;
    }
  }
  Tree root;

  // [L, R)
  this(TX L, TX R) {
    assert(L < R, "LiChaoTree: L < R must hold");
    this.L = L;
    this.R = R;
  }
  // Add g to the whole [L, R)
  void add(Func g) {
    root = add(root, L, R, g);
  }
  // Add g to [a, b)
  void add(TX a, TX b, Func g) {
    root = add(root, L, R, a, b, g);
  }
  // Get max at a
  TY opCall(TX a) {
    TY mx = TY.min;
    TX l = L, r = R;
    for (Tree u = root; u; ) {
      if (u.f) {
        const fX = u.f(a);
        if (mx < fX) mx = fX;
      }
      const mid = l + (r - l) / 2;
      if (a < mid) {
        u = u.l; r = mid;
      } else {
        u = u.r; l = mid;
      }
    }
    return mx;
  }

 private:
  Tree add(Tree u0, TX l, TX r, Func g) {
    if (!u0) return new Tree(g);
    TY gL = g(l), gR = g(r);
    for (Tree u = u0; ; ) {
      TY fL = u.f ? u.f(l) : TY.min, fR = u.f ? u.f(r) : TY.min;
      if (fL >= gL && fR >= gR) return u0;
      if (fL <= gL && fR <= gR) { u.f = g; return u0; }
      if (r - l == 1) { if (fL <= gL) { u.f = g; } return u0; }
      const mid = l + (r - l) / 2;
      TY fMid = u.f(mid), gMid = g(mid);
      if (fMid < gMid) {
        swap(u.f, g);
        if (!g) return u0;
        if (gL < fL) {
          if (!u.l) { u.l = new Tree(g); return u0; }
          u = u.l; r = mid; gL = fL; gR = fMid;
        } else {
          if (!u.r) { u.r = new Tree(g); return u0; }
          u = u.r; l = mid; gL = fMid; gR = fR;
        }
      } else {
        if (fL < gL) {
          if (!u.l) { u.l = new Tree(g); return u0; }
          u = u.l; r = mid; gR = gMid;
        } else {
          if (!u.r) { u.r = new Tree(g); return u0; }
          u = u.r; l = mid; gL = gMid;
        }
      }
    }
  }
  Tree add(Tree u, TX l, TX r, TX a, TX b, Func g) {
    if (b <= l || r <= a) return u;
    if (a <= l && r <= b) return add(u, l, r, g);
    if (u && u.f && u.f(l) >= g(l) && u.f(r) >= g(r)) return u;
    if (!u) u = new Tree(null);
    const mid = l + (r - l) / 2;
    u.l = add(u.l, l, mid, a, b, g);
    u.r = add(u.r, mid, r, a, b, g);
    return u;
  }
}

// y = p x + q
class Line {
  alias TX = long;
  alias TY = long;
  long p, q;
  this(long p, long q) {
    this.p = p;
    this.q = q;
  }
  TY opCall(TX x) {
    return p * x + q;
  }
}


enum INF = 10L^^18;
enum LIM_CN = 10L^^12 + 10L^^11 + 10;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const C = readLong();
      auto A = new long[N];
      auto B = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
        B[i] = readLong();
      }
      
      /*
        min_{j<=i} (dp[j] + A[i] (i - j) + C (i - j) (i - j + 1) / 2)
        = min_{j<=i} ((dp[j] + C j (j - 1) / 2) - j (A[i] + C i))
            + A[i] i + C i (i + 1) / 2
        
        min_i (DP[i] + A[i] (j - i - 1) + C (j - i - 1) (j - i) / 2)
        = min_i ((DP[i] - A[i] (i + 1) + C i (i + 1) / 2) + (A[i] - C i) j)
            + C j (j - 1) / 2
      */
      auto lct = new LiChaoTree!Line(-LIM_CN, LIM_CN);
      auto LCT = new LiChaoTree!Line(0, N + 1);
      void add(int j, long val) {
        lct.add(new Line(-(-j), -(val + C * j * (j - 1) / 2)));
      }
      void ADD(int i, long val) {
        LCT.add(new Line(-(A[i] - C * i), -(val - A[i] * (i + 1) + C * i * (i + 1) / 2)));
      }
      
      auto dp = new long[N + 1];
      dp[] = INF;
      foreach (i; 0 .. N + 1) {
        dp[i] = (i == 0) ? 0 : (-LCT(i) + C * i * (i - 1) / 2);
        add(i, dp[i]);
        if (i == N) {
          break;
        }
        long mn = -lct(A[i] + C * i) + A[i] * i + C * i * (i + 1) / 2;
        debug {
          writeln(i, ": ", mn, " ", mn + A[i] + B[i]);
        }
        ADD(i, mn + A[i] + B[i]);
      }
      debug {
        writeln("dp = ", dp);
      }
      writeln(dp[N]);
      
      debug {
        auto brt = new long[N + 1];
        brt[] = INF;
        brt[0] = 0;
        foreach (i; 0 .. N) {
          long mn = INF;
          foreach (j; 0 .. i + 1) {
            // [j, i)
            const long l = i - j;
            chmin(mn, brt[j] + A[i] * l + C * (l * (l + 1) / 2));
          }
          writeln(i, ": ", mn, " ", mn + A[i] + B[i]);
          mn += A[i] + B[i];
          foreach (j; i + 1 .. N + 1) {
            // [i + 1, j)
            const long l = j - (i + 1);
            chmin(brt[j], mn + A[i] * l + C * (l * (l + 1) / 2));
          }
        }
        writeln("brt = ", brt);
      }
    }
  } catch (EOFException e) {
  }
}
0