結果

問題 No.209 Longest Mountain Subsequence
ユーザー te-shte-sh
提出日時 2016-09-16 13:53:28
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,210 bytes
コンパイル時間 1,337 ms
コンパイル使用メモリ 107,836 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-09-02 22:14:52
合計ジャッジ時間 2,080 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
4,368 KB
testcase_01 AC 44 ms
4,372 KB
testcase_02 AC 42 ms
4,372 KB
testcase_03 AC 153 ms
4,372 KB
testcase_04 AC 153 ms
4,368 KB
testcase_05 AC 21 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;

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

  foreach (_; 0..t) {
    auto n = readln.chomp.to!size_t;
    auto ai = readln.split.map!(to!int).array;
    writeln(calc(n, ai));
  }
}

int calc(size_t n, int[] ai)
{
  auto memo = new int[][](n, n);

  int dp(size_t i1, size_t i2 = size_t.max) {
    auto maxR = 1;
    if (i2 == size_t.max) {
      foreach (j; i1+1..n)
        maxR = max(maxR, dp(j, i1) + 1);
    } else {
      if (memo[i1][i2] > 0) {
        maxR = memo[i1][i2];
      } else {
        auto d = ai[i1] - ai[i2];
        if (d > 0) {
          foreach (j; i1+1..n)
            if (ai[j] - ai[i1] > d || ai[j] - ai[i1] < 0)
              maxR = max(maxR, dp(j, i1) + 1);
        } else {
          foreach (j; i1+1..n)
            if (ai[j] - ai[i1] > d && ai[j] - ai[i1] < 0)
              maxR = max(maxR, dp(j, i1) + 1);
        }
        memo[i1][i2] = maxR;
      }
    }
    return maxR;
  }

  auto maxR = 0;
  foreach (i; 0..n)
    maxR = max(maxR, dp(i));

  return maxR;
}
0