結果

問題 No.209 Longest Mountain Subsequence
ユーザー te-shte-sh
提出日時 2017-05-12 12:55:46
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 938 bytes
コンパイル時間 608 ms
コンパイル使用メモリ 89,892 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 13:21:30
合計ジャッジ時間 1,935 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
4,380 KB
testcase_01 AC 60 ms
4,380 KB
testcase_02 AC 56 ms
4,376 KB
testcase_03 AC 190 ms
4,380 KB
testcase_04 AC 190 ms
4,380 KB
testcase_05 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto t = readln.chomp.to!size_t;

  foreach (_; 0..t) {
    auto n = readln.chomp.to!size_t;
    auto ai = readln.split.to!(int[]);

    auto dp1 = new int[int][](n);
    auto dp2 = new int[int][](n);

    int maxR(size_t i, int d)
    {
      if (d in dp1[i]) return dp1[i][d];
      auto r = 0;
      foreach (j; i+1..n) {
        auto d2 = ai[i] - ai[j];
        if (d2 >= 0 && d2 < d)
          r = max(r, maxR(j, min(d2, ai[j])) + 1);
      }
      return dp1[i][d] = r;
    }

    int maxL(size_t i, int d)
    {
      if (d in dp2[i]) return dp2[i][d];
      auto r = 0;
      foreach_reverse (j; 0..i) {
        auto d2 = ai[i] - ai[j];
        if (d2 >= 0 && d2 < d)
          r = max(r, maxL(j, min(d2, ai[j])) + 1);
      }
      return dp2[i][d] = r;
    }

    writeln(n.iota.map!(i => maxR(i, ai[i]) + maxL(i, ai[i]) + 1).maxElement);
  }
}
0