結果

問題 No.127 門松もどき
ユーザー te-shte-sh
提出日時 2017-04-26 14:21:44
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 1,030 bytes
コンパイル時間 672 ms
コンパイル使用メモリ 92,236 KB
実行使用メモリ 80,972 KB
最終ジャッジ日時 2023-09-03 13:00:31
合計ジャッジ時間 7,275 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto n = readln.chomp.to!size_t;
  auto ai = readln.split.to!(int[]);

  auto calc = new Calc(n, ai);
  auto r = n.iota.map!(i => [calc.calcA(i, n-1), calc.calcB(i, 0)]).joiner.maxElement;
  writeln(r);
}

class Calc
{
  size_t n;
  int[] ai;
  int[][] dpa, dpb;

  this(size_t n, int[] ai)
  {
    this.n = n;
    this.ai = ai;
    dpa = new int[][](n, n);
    dpb = new int[][](n, n);
  }

  int calcA(size_t i, size_t m)
  {
    if (dpa[i][m]) return dpa[i][m];

    auto a = ai[i], b = int.max, r = 0;

    foreach_reverse (j; i+1..m+1)
      if (ai[j] > a && ai[j] < b) {
        b = ai[j];
        r = max(r, calcB(j, i+1));
      }

    return dpa[i][m] = r + 1;
  }

  int calcB(size_t i, size_t m)
  {
    if (dpb[i][m]) return dpb[i][m];

    auto a = ai[i], b = int.max, r = 0;

    foreach (j; m..i)
      if (ai[j] > a && ai[j] < b) {
        b = ai[j];
        r = max(r, calcA(j, i-1));
      }

    return dpb[i][m] = r + 1;
  }
}
0