結果

問題 No.335 門松宝くじ
ユーザー te-shte-sh
提出日時 2017-07-05 17:21:04
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,311 bytes
コンパイル時間 1,438 ms
コンパイル使用メモリ 157,536 KB
実行使用メモリ 11,340 KB
最終ジャッジ日時 2023-09-03 14:59:52
合計ジャッジ時間 5,096 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 83 ms
10,172 KB
testcase_06 AC 163 ms
11,260 KB
testcase_07 AC 216 ms
11,240 KB
testcase_08 AC 161 ms
11,232 KB
testcase_09 AC 430 ms
11,200 KB
testcase_10 AC 429 ms
11,284 KB
testcase_11 WA -
testcase_12 AC 425 ms
11,208 KB
testcase_13 AC 421 ms
11,340 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 = 0;

  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.front < ei ? ej : !c.empty && c.back > ej ? c.back : 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.front < ej ? ei : !c.empty && c.back > ei ? c.back : 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