結果

問題 No.209 Longest Mountain Subsequence
ユーザー simansiman
提出日時 2023-02-15 10:41:43
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 1,791 bytes
コンパイル時間 4,065 ms
コンパイル使用メモリ 108,060 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-24 20:28:18
合計ジャッジ時間 5,370 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

void solver() {
  int N;
  cin >> N;
  vector<ll> A(N);

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  vector<vector<ll>> dp1(N, vector<ll>(N + 1, LLONG_MAX));

  for (int i = 0; i < N; ++i) {
    dp1[i][1] = 0;
    ll a1 = A[i];

    for (int h = 0; h < N; ++h) {
      if (dp1[i][h] == LLONG_MAX) continue;

      for (int j = i + 1; j < N; ++j) {
        ll a2 = A[j];
        ll diff = abs(a1 - a2);
        if (a1 >= a2) continue;
        if (dp1[i][h] >= diff) continue;

        dp1[j][h + 1] = min(dp1[j][h + 1], diff);
      }
    }
  }

  vector<vector<ll>> dp2(N, vector<ll>(N + 1, LLONG_MAX));

  for (int i = N - 1; i >= 0; --i) {
    dp2[i][1] = 0;
    ll a1 = A[i];

    for (int h = 0; h < N; ++h) {
      if (dp2[i][h] == LLONG_MAX) continue;

      for (int j = i - 1; j >= 0; --j) {
        ll a2 = A[j];
        ll diff = abs(a1 - a2);
        if (a2 <= a1) continue;
        if (dp2[i][h] >= diff) continue;

        dp2[j][h + 1] = min(dp2[j][h + 1], diff);
      }
    }
  }

  int ans = 1;

  for (int i = 0; i < N; ++i) {
    for (int h1 = 1; h1 <= N; ++h1) {
      for (int h2 = 1; h2 <= N; ++h2) {
        if (dp1[i][h1] != LLONG_MAX && dp2[i][h2] != LLONG_MAX) {
          ans = max(ans, h1 + h2 - 1);
        } else if (dp1[i][h1] != LLONG_MAX) {
          ans = max(ans, h1);
        } else if (dp2[i][h2] != LLONG_MAX) {
          ans = max(ans, h2);
        }
      }
    }
  }

  cout << ans << endl;
}

int main() {
  int T;
  cin >> T;

  for (int i = 0; i < T; ++i) {
    solver();
  }

  return 0;
}
0