結果

問題 No.1297 銅像
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-11-24 14:01:45
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 4,407 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 107,456 KB
実行使用メモリ 13,956 KB
最終ジャッジ日時 2023-09-04 11:12:20
合計ジャッジ時間 3,518 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,500 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 80 ms
6,676 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 97 ms
6,680 KB
testcase_18 AC 99 ms
6,716 KB
testcase_19 AC 95 ms
13,956 KB
testcase_20 AC 112 ms
6,404 KB
testcase_21 AC 113 ms
7,480 KB
testcase_22 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)); }


// floor(a / b)
Int divFloor(Int)(Int a, Int b) {
  return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);
}

// ceil(a / b)
Int divCeil(Int)(Int a, Int b) {
  return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);
}


enum INF = 10L^^18;

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();
      }
      
      /*
        C k^2
        + (A[i] - A[j] - C (i + j + 1)) k
        + (-A[i] * (i + 1) + A[j] * j + C (i * (i + 1) / 2 + j * (j + 1) / 2))
      */
      long calc(int i, int j) {
        const p = A[i] - A[j] - C * (i + j + 1);
        const q = -A[i] * (i + 1) + A[j] * j + C * (1L * i * (i + 1) / 2 + 1L * j * (j + 1) / 2);
        long mn = INF;
        void check(long k) {
          if (i + 1 <= k && k <= j) {
            chmin(mn, (C * k + p) * k + q);
          }
        }
        check(i + 1);
        check(j);
        check(divFloor(-p, 2 * C));
        check(divCeil(-p, 2 * C));
        return mn;
      }
      
      auto dp = new long[N];
      foreach (i; 0 .. N) {
        dp[i] = A[i] * i + C * i * (i + 1) / 2;
      }
      auto stack = new int[N];
      int len;
      foreach (j; 0 .. N) {
        long here(int i) {
          return dp[i] + calc(i, j);
        }
        for (; len >= 2 && here(stack[len - 2]) <= here(stack[len - 1]); --len) {}
        if (len >= 1) {
          chmin(dp[j], here(stack[len - 1]));
        }
        dp[j] += A[j] + B[j];
        if (j + 1 < N) {
          stack[len++] = j;
        }
      }
      debug {
        writeln("dp = ", dp);
      }
      long ans = INF;
      foreach (i; 0 .. N) {
        chmin(ans, dp[i] + A[i] * (N - i - 1) + C * (N - i - 1) * (N - i) / 2);
      }
      writeln(ans);
      
      debug {
        long calcBrute(int i, int j) {
          long mn = INF;
          foreach (k; i + 1 .. j + 1) {
            long cost;
            cost += A[i] * (k - i - 1) + C * (k - i - 1) * (k - i) / 2;
            cost += A[j] * (j - k) + C * (j - k) * (j - k + 1) / 2;
            chmin(mn, cost);
          }
          return mn;
        }
        
        foreach (i; 0 .. N) foreach (j; i + 1 .. N) {
          assert(calc(i, j) == calcBrute(i, j));
        }
        
        auto brt = new long[N];
        brt[] = INF;
        foreach (i; 0 .. N) {
          chmin(brt[i], A[i] * i + C * i * (i + 1) / 2);
        }
        foreach (i; 0 .. N) {
          brt[i] += A[i] + B[i];
          foreach (j; i + 1 .. N) {
            chmin(brt[j], brt[i] + calcBrute(i, j));
          }
        }
        writeln("brt = ", brt);
        long brtAns = INF;
        foreach (i; 0 .. N) {
          chmin(brtAns, brt[i] + A[i] * (N - i - 1) + C * (N - i - 1) * (N - i) / 2);
        }
        writeln("brtAns = ", brtAns);
        
        foreach (i; 0 .. N) foreach (j; i + 1 .. N) {
          foreach (k; j + 1 .. N - 1) {
            if (brt[i] + calcBrute(i, k) >= brt[j] + calcBrute(j, k)) {
              assert(brt[i] + calcBrute(i, k + 1) >= brt[j] + calcBrute(j, k + 1));
            }
          }
        }
      }
    }
  } catch (EOFException e) {
  }
}
0