結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 87 ms
9,224 KB
testcase_06 AC 171 ms
11,256 KB
testcase_07 AC 227 ms
11,272 KB
testcase_08 AC 155 ms
11,268 KB
testcase_09 AC 452 ms
11,268 KB
testcase_10 AC 449 ms
11,244 KB
testcase_11 AC 452 ms
11,256 KB
testcase_12 AC 451 ms
11,268 KB
testcase_13 AC 451 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

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 l = redBlackTree!int(), c = redBlackTree!int(), r = redBlackTree(e[2..$]);
  auto s = 0L;

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

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

      if (ei < ej) {
        auto r1 = !l.empty && l.back > ei ? max(l.back, ej) : 0;
        auto r2 = !c.empty && c.back > ej ? c.back : !c.empty && c.front < ei ? ej : 0;
        auto r3 = !r.empty && r.front < ej ? ej : 0;
        s += max(r1, r2, r3);
      } else {
        auto r1 = !l.empty && l.front < ei ? ei : 0;
        auto r2 = !c.empty && c.back > ei ? c.back : !c.empty && c.front < ej ? ei : 0;
        auto r3 = !r.empty && r.back > ej ? max(r.back, ei) : 0;
        s += max(r1, r2, r3);
      }

      if (j < n-1) {
        c.insert(ej);
        r.removeKey(e[j+1]);
      }
    }

    l.insert(ei);
    r = c;
    if (i < n-2) {
      r.insert(e[$-1]);
      r.removeKey(e[i+1]);
      r.removeKey(e[i+2]);
    }
    c = redBlackTree!int();
  }

  return s;
}
0