結果

問題 No.209 Longest Mountain Subsequence
ユーザー pekempeypekempey
提出日時 2015-05-15 23:48:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,400 bytes
コンパイル時間 1,181 ms
コンパイル使用メモリ 145,468 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 08:45:44
合計ジャッジ時間 2,030 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define fr(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

int N;
const int maxn = 100;
ll A[maxn];
ll dp[maxn][maxn]; // last a[i], last last a[i];
ll xdp[maxn];
ll dp2[maxn][maxn];
ll ydp[maxn];

void solve() {
    rep (i, maxn) rep (j, maxn) dp[i][j] = 0;

    rep (i, N) {
        rep (j, i) {
            if (A[j] < A[i]) dp[i][j] = 1;
            else dp[i][j] = 0;
            rep (k, j) {
                if ((A[j] < A[i] && A[k] < A[j]) && abs(A[k] - A[j]) < abs(A[j] - A[i])) {
                    if (dp[i][j] < dp[j][k] + 1) {
                        dp[i][j] = dp[j][k] + 1;
                    }
                }
            }
        }
    }

    rep (i, N) {
        xdp[i] = 0;
        rep (j, N) {
            xdp[i] = max(xdp[i], dp[i][j]);
        }
    }
}

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

    rep (k, T) {
        cin >> N;

        rep (i, N) {
            cin >> A[i];
        }

        solve();

        rep (i, N) {
            ydp[i] = xdp[i];
        }

        reverse(A, A + N);

        solve();

        reverse(xdp, xdp + N);

        ll ans = 0;

        rep (i, N) {
            ans = max(ans, xdp[i] + ydp[i] + 1);
        }

        cout << ans << endl;
    }
}
0