結果

問題 No.448 ゆきこーだーの雨と雪 (3)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-06 17:34:11
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 2,672 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 114,080 KB
実行使用メモリ 36,864 KB
最終ジャッジ日時 2023-09-04 00:23:07
合計ジャッジ時間 7,738 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 32 ms
7,268 KB
testcase_14 AC 36 ms
7,836 KB
testcase_15 AC 91 ms
19,780 KB
testcase_16 AC 156 ms
24,588 KB
testcase_17 AC 159 ms
24,732 KB
testcase_18 AC 26 ms
7,316 KB
testcase_19 AC 212 ms
33,148 KB
testcase_20 AC 76 ms
17,092 KB
testcase_21 AC 210 ms
32,980 KB
testcase_22 AC 76 ms
14,040 KB
testcase_23 AC 211 ms
32,864 KB
testcase_24 AC 52 ms
14,452 KB
testcase_25 AC 204 ms
26,340 KB
testcase_26 AC 25 ms
7,260 KB
testcase_27 AC 156 ms
24,796 KB
testcase_28 AC 145 ms
21,408 KB
testcase_29 AC 80 ms
14,556 KB
testcase_30 AC 221 ms
31,996 KB
testcase_31 AC 31 ms
7,564 KB
testcase_32 AC 34 ms
7,804 KB
testcase_33 AC 234 ms
31,456 KB
testcase_34 AC 220 ms
31,364 KB
testcase_35 AC 233 ms
31,940 KB
testcase_36 AC 223 ms
32,336 KB
testcase_37 AC 229 ms
23,432 KB
testcase_38 AC 225 ms
36,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, 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;

int N;
long K;
long[] T, D;

long solve(long dLim) {
  debug {
    writeln("solve ", dLim);
  }
  int[] xs;
  xs ~= 0;
  foreach (i; 1 .. N + 1) {
    if (D[i] > dLim) {
      xs ~= i;
    }
  }
  xs ~= N + 1;
  const len = cast(int)(xs.length) - 1;
  debug {
    writeln("  xs = ", xs);
  }
  long ans = D.sum;
  auto dp = new long[N + 2];
  foreach (j; 0 .. len) {
    if (T[xs[j + 1]] - T[xs[j]] < K) {
      return INF;
    }
    dp[xs[j]] = 0;
    long dpMax = -1;
    for (int l = xs[j], r = xs[j] + 1; r <= xs[j + 1]; ++r) {
      for (; T[r] - T[l] >= K; ++l) {
        chmax(dpMax, dp[l]);
      }
      dp[r] = (dpMax >= 0) ? (dpMax + D[r]) : -1;
    }
    debug {
      writefln("  dp[%s, %s] = %s", xs[j], xs[j + 1], dp[xs[j] .. xs[j + 1] + 1]);
    }
    assert(dp[xs[j + 1]] >= 0);
    ans -= dp[xs[j + 1]];
  }
  return ans;
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      K = readLong();
      T = new long[N + 2];
      D = new long[N + 2];
      foreach (i; 1 .. N + 1) {
        T[i] = readLong();
        D[i] = readLong();
      }
      
      T[0] = T[1] - K;
      T[N + 1] = T[N] + K;
      D[0] = D[N + 1] = 0;
      
      auto ds = D.dup;
      ds = ds.sort.uniq.array;
      int lo = -1, hi = cast(int)(ds.length) - 1;
      for (; lo + 1 < hi; ) {
        const mid = (lo + hi) / 2;
        (solve(ds[mid]) < INF) ? (hi = mid) : (lo = mid);
      }
      
      const ans = solve(ds[hi]);
      writeln(ds[hi]);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0