結果

問題 No.913 木の燃やし方
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-10-19 02:04:23
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 230 ms / 3,000 ms
コード長 3,436 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 122,368 KB
実行使用メモリ 42,328 KB
最終ジャッジ日時 2024-06-22 02:53:35
合計ジャッジ時間 10,427 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 218 ms
20,812 KB
testcase_09 AC 222 ms
20,772 KB
testcase_10 AC 212 ms
19,684 KB
testcase_11 AC 215 ms
19,748 KB
testcase_12 AC 230 ms
20,748 KB
testcase_13 AC 212 ms
20,956 KB
testcase_14 AC 205 ms
20,288 KB
testcase_15 AC 203 ms
20,908 KB
testcase_16 AC 211 ms
20,404 KB
testcase_17 AC 203 ms
20,480 KB
testcase_18 AC 208 ms
20,064 KB
testcase_19 AC 170 ms
42,152 KB
testcase_20 AC 199 ms
41,740 KB
testcase_21 AC 216 ms
20,516 KB
testcase_22 AC 213 ms
20,044 KB
testcase_23 AC 210 ms
20,276 KB
testcase_24 AC 171 ms
30,756 KB
testcase_25 AC 147 ms
29,960 KB
testcase_26 AC 210 ms
42,328 KB
testcase_27 AC 201 ms
20,524 KB
testcase_28 AC 217 ms
34,484 KB
testcase_29 AC 226 ms
20,932 KB
testcase_30 AC 223 ms
20,092 KB
testcase_31 AC 176 ms
41,664 KB
testcase_32 AC 191 ms
20,224 KB
testcase_33 AC 187 ms
21,700 KB
testcase_34 AC 212 ms
21,092 KB
testcase_35 AC 195 ms
34,256 KB
testcase_36 AC 199 ms
20,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int N;
long[] A;

long[] ASum;
long[] ans;

/*
  ((j - i)^2 + (ASum[j] - ASum[i]))
  (i^2 - ASum[i]) + min_j ((j^2 + ASum[j]) + (-2 j) i)
  (j^2 + ASum[j]) + min_i ((i^2 - ASum[i]) + (2 i) (-j))
*/

void solve(int l, int r) {
  if (l >= r) {
    return;
  }
  // l <= i <= mid, mid + 1 <= j <= r
  const mid = (l + r) / 2;
  solve(l, mid);
  solve(mid + 1, r);
  
  // [1]: decreasing
  auto q = new Tuple!(long, long)[max(mid - l + 1, r - mid + 1)];
  int qb, qe;
  long mn;
  
  qb = qe = 0;
  foreach (j; mid + 1 .. r + 1) {
    const t = tuple(1L * j * j + ASum[j], -2L * j);
    for (; qe - qb >= 2 && (q[qe - 1][0] - q[qe - 2][0]) * -(t[1] - q[qe - 2][1]) >= (t[0] - q[qe - 2][0]) * -(q[qe - 1][1] - q[qe - 2][1]); --qe) {}
    q[qe++] = t;
  }
  debug {
    writeln("q[qb .. qe] = ", q[qb .. qe]);
  }
  mn = INF;
  foreach (i; l .. mid + 1) {
    for (; qe - qb >= 2 && (q[qb][0] + q[qb][1] * i >= q[qb + 1][0] + q[qb + 1][1] * i); ++qb) {}
    const tmp = 1L * i * i - ASum[i] + (q[qb][0] + q[qb][1] * i);
    debug {
      // writefln("i = %s, mid = %s, r = %s: %s", i, mid, r, tmp);
    }
    chmin(mn, tmp);
    chmin(ans[i], mn);
  }
  
  qb = qe = 0;
  foreach_reverse (i; l .. mid + 1) {
    const t = tuple(1L * i * i - ASum[i], 2L * i);
    for (; qe - qb >= 2 && (q[qe - 1][0] - q[qe - 2][0]) * -(t[1] - q[qe - 2][1]) >= (t[0] - q[qe - 2][0]) * -(q[qe - 1][1] - q[qe - 2][1]); --qe) {}
    q[qe++] = t;
  }
  debug {
    writeln("q[qb .. qe] = ", q[qb .. qe]);
  }
  mn = INF;
  foreach_reverse (j; mid + 1 .. r + 1) {
    for (; qe - qb >= 2 && (q[qb][0] + q[qb][1] * (-j) >= q[qb + 1][0] + q[qb + 1][1] * (-j)); ++qb) {}
    const tmp = 1L * j * j + ASum[j] + (q[qb][0] + q[qb][1] * (-j));
    debug {
      writefln("l = %s, mid = %s, j = %s: %s", l, mid, j, tmp);
    }
    chmin(mn, tmp);
    chmin(ans[j - 1], mn);
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }
      
      ASum = new long[N + 1];
      foreach (i; 0 .. N) {
        ASum[i + 1] = ASum[i] + A[i];
      }
      
      ans = new long[N];
      ans[] = INF;
      solve(0, N);
      foreach (i; 0 .. N) {
        writeln(ans[i]);
      }
    }
  } catch (EOFException e) {
  }
}
0