結果

問題 No.209 Longest Mountain Subsequence
ユーザー airisairis
提出日時 2015-05-16 20:07:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 2,501 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 75,600 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 09:18:52
合計ジャッジ時間 1,413 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <numeric>
#include <bitset>
#define rep(x, to) for (int x = 0; x < (to); x++)
#define REP(x, a, to) for (int x = (a); x < (to); x++)
#define foreach(itr, x) for (typeof((x).begin()) itr = (x).begin(); itr != (x).end(); itr++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;

int N, T;
int a[105];

int dp[105][105];
int dp2[105][105];

int dfs(int i, int j, int tdp[105][105]) {
	if (tdp[i][j] != 0) return tdp[i][j];
	int res = 1;
	int k_not_exist = 1;
	for (int k = 0; k < j; k++) {
		if (a[j] < a[i] && a[k] < a[j] && a[i]-a[j] > a[j]-a[k]) {
			res = max(res, 1 + dfs(j, k, tdp));
			k_not_exist = 0;
		}
	}
	res += k_not_exist;
	return tdp[i][j] = res;
}

int main() {
	cin >> T;
	while (T--) {
		int ans = 1;
		memset(dp, 0, sizeof(dp));
		memset(dp2, 0, sizeof(dp2));
		cin >> N;
		rep(i, N) cin >> a[i];

		//up
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (i == j) dp[i][j] = 1;
				if (a[i] < a[j]) dp[i][j] = 1;
			}
		}

		for (int j = 0; j < N; j++) {
			for (int i = 0; i <= j; i++) {
				if (a[i] >= a[j]) continue;
				for (int k = 0; k < i; k++) {
					if (a[i]-a[k] < a[j]-a[i]) {
						dp[i][j] = max(dp[i][j], dp[k][i]);
					}
				}
				dp[i][j] += 1;
			}
		}
#if 0
		puts("up");
		rep(i, 5) rep(j, 5) {
			printf("%d%c", dp[i][j], j == 5-1 ? '\n' : ' ');
		}
#endif

		//down
		reverse(a, a+N);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (i == j) dp2[i][j] = 1;
				if (a[i] < a[j]) dp2[i][j] = 1;
			}
		}
		for (int j = 0; j < N; j++) {
			for (int i = 0; i <= j; i++) {
				if (a[i] >= a[j]) continue;
				for (int k = 0; k < i; k++) {
					if (a[i]-a[k] < a[j]-a[i]) {
						dp2[i][j] = max(dp2[i][j], dp2[k][i]);
					}
				}
				dp2[i][j] += 1;
			}
		}
#if 0
		puts("down");
		rep(i, 5) rep(j, 5) {
			printf("%d%c", dp2[i][j], j == 5-1 ? '\n' : ' ');
		}
#endif



		for (int j = 0; j < N; j++) {
			for (int i = 0; i <= j; i++) {
				for (int k = 0; k <= N-j-1; k++) {
					if (dp[i][j] + dp2[k][N-j-1] - 1 > ans) {
						ans = dp[i][j] + dp2[k][N-j-1]-1;
						//printf("dp[%d][%d]+dp2[%d][%d]-1=%d+%d-1=%d\n",i,j,k,N-j-1,dp[i][j],dp2[k][N-j-1],dp[i][j]+dp2[k][N-j-1]-1);
					}
				}
			}
		}

	cout << ans << endl;
	}
	return 0;
}

0