結果

問題 No.209 Longest Mountain Subsequence
ユーザー motimoti
提出日時 2015-07-18 14:36:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,266 bytes
コンパイル時間 563 ms
コンパイル使用メモリ 74,416 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 19:00:09
合計ジャッジ時間 1,498 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <set>
#include <map>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int T, N, A[110];
int dpl[110][110], dpr[110][110];

int left(int l, int r) {
  auto& ret = dpl[l][r];
  if(ret >= 0) { return ret; }
  ret = 0;
  rep(i, l) {
    if(A[i] < A[l] && A[l] - A[i] < A[r] - A[l]) {
      ret = max(ret, left(i, l) + 1);
    }
  }
  return ret;
}

int right(int l, int r) {
  auto& ret = dpr[l][r];
  if(ret >= 0) { return ret; }
  ret = 0;
  REP(i, r+1, N) {
    if(A[r] > A[i] && A[l] - A[r] > A[r] - A[i]) {
      ret = max(ret, right(r, i) + 1);
    }
  }
  return ret;
}

int solve(int idx) {
  int lmax = 0, rmax = 0;
  rep(i, idx) {
    if(A[i] < A[idx]) {
      lmax = max(lmax, left(i, idx) + 1);
    }
  }
  REP(i, idx+1, N) {
    if(A[idx] > A[i]) {
      rmax = max(rmax, right(idx, i) + 1);
    }
  }
  return lmax + rmax + 1;
}

int main() {

  cin >> T;
  while(T--) {
    cin >> N; rep(i, N) cin >> A[i];
    rep(i, 110) rep(j, 110) dpl[i][j] = dpr[i][j] = -1;
    int ans = 0;
    rep(i, N) ans = max(ans, solve(i));
    cout << ans << endl;
  }

  return 0;
}
0