結果

問題 No.127 門松もどき
ユーザー te-shte-sh
提出日時 2017-04-26 15:41:34
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 253 ms / 5,000 ms
コード長 941 bytes
コンパイル時間 765 ms
コンパイル使用メモリ 87,784 KB
実行使用メモリ 75,968 KB
最終ジャッジ日時 2023-09-03 13:01:05
合計ジャッジ時間 4,283 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 253 ms
75,968 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 19 ms
10,628 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 117 ms
56,828 KB
testcase_13 AC 145 ms
64,292 KB
testcase_14 AC 139 ms
61,524 KB
testcase_15 AC 188 ms
72,876 KB
testcase_16 AC 125 ms
59,904 KB
testcase_17 AC 139 ms
64,440 KB
testcase_18 AC 116 ms
59,128 KB
testcase_19 AC 65 ms
32,384 KB
testcase_20 AC 69 ms
32,680 KB
testcase_21 AC 32 ms
22,596 KB
testcase_22 AC 236 ms
75,752 KB
testcase_23 AC 214 ms
75,068 KB
testcase_24 AC 183 ms
68,080 KB
testcase_25 AC 214 ms
72,576 KB
testcase_26 AC 104 ms
36,768 KB
権限があれば一括ダウンロードができます

ソースコード

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 = 0;
  foreach (i; 0..n) {
    r = max(r, calc.calcA(i, n-1));
    r = max(r, calc.calcB(i, 0));
  }

  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 j)
  {
    if (i == j) return 1;
    if (dpa[i][j]) return dpa[i][j];

    auto r = calcA(i, j-1);
    if (ai[i] < ai[j])
      r = max(r, calcB(j, i+1) + 1);

    return dpa[i][j] = r;
  }

  int calcB(size_t i, size_t j)
  {
    if (i == j) return 1;
    if (dpb[i][j]) return dpb[i][j];

    auto r = calcB(i, j+1);
    if (ai[i] < ai[j])
      r = max(r, calcA(j, i-1) + 1);

    return dpb[i][j] = r;
  }
}
0