結果

問題 No.335 門松宝くじ
ユーザー te-shte-sh
提出日時 2017-07-05 17:52:16
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,975 bytes
コンパイル時間 686 ms
コンパイル使用メモリ 93,616 KB
実行使用メモリ 5,076 KB
最終ジャッジ日時 2023-09-03 15:00:18
合計ジャッジ時間 1,856 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 13 ms
4,376 KB
testcase_06 AC 24 ms
4,380 KB
testcase_07 AC 30 ms
4,376 KB
testcase_08 AC 22 ms
4,380 KB
testcase_09 AC 59 ms
4,380 KB
testcase_10 AC 58 ms
4,376 KB
testcase_11 AC 59 ms
4,384 KB
testcase_12 AC 59 ms
4,380 KB
testcase_13 AC 57 ms
5,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto rd = readln.split.to!(size_t[]), n = rd[0], m = rd[1];
  auto e = m.iota.map!(_ => readln.split.to!(int[])).array;

  writeln(e.map!(ei => calcE(n, ei)).maxIndex);
}

auto calcE(size_t n, int[] e)
{
  auto minSt = SparseTable!(int, min)(e);
  auto maxSt = SparseTable!(int, max)(e);
  auto s = 0L;

  foreach (i; 0..n-1)
    foreach (j; i+1..n) {
      auto ei = e[i], ej = e[j];

      if (ei < ej) {
        auto r1 = i > 0 && maxSt[0..i] > ei ? max(maxSt[0..i], ej) : 0;
        auto r2 = j > i+1 && maxSt[i+1..j] > ej ? maxSt[i+1..j] : j > i+1 && minSt[i+1..j] < ei ? ej : 0;
        auto r3 = j < n-1 && minSt[j+1..$] < ej ? ej : 0;
        s += max(r1, r2, r3);
      } else {
        auto r1 = i > 0 && minSt[0..i] < ei ? ei : 0;
        auto r2 = j > i+1 && maxSt[i+1..j] > ei ? maxSt[i+1..j] : j > i+1 && minSt[i+1..j] < ej ? ei : 0;
        auto r3 = j < n-1 && maxSt[j+1..$] > ej ? max(maxSt[j+1..$], ei) : 0;
        s += max(r1, r2, r3);
      }
    }

  return s;
}

struct SparseTable(T, alias pred = "a < b ? a : b")
{
  import std.algorithm, std.functional;
  alias predFun = binaryFun!pred;

  size_t[] logTable;
  size_t[][] rmq;
  size_t n;
  T[] a;

  this(T[] a)
  {
    this.a = a;
    this.n = a.length;

    logTable = new size_t[n + 1];
    foreach (i; 2..n+1)
      logTable[i] = logTable[i >> 1] + 1;

    rmq = new size_t[][](logTable[n] + 1, n);

    foreach (i; 0..n)
      rmq[0][i] = i;

    for (size_t k = 1; (1 << k) < n; ++k)
      for (size_t i = 0; i + (1 << k) <= n; ++i) {
        auto x = rmq[k - 1][i];
        auto y = rmq[k - 1][i + (1 << k - 1)];
        rmq[k][i] = predFun(a[x], a[y]) == a[x] ? x : y;
      }
  }

  pure size_t opDollar() const { return n; }

  pure T opSlice(size_t l, size_t r) const
  {
    auto k = logTable[r - l - 1];
    auto x = rmq[k][l];
    auto y = rmq[k][r - (1 << k)];
    return predFun(a[x], a[y]);
  }
}
0