結果

問題 No.209 Longest Mountain Subsequence
ユーザー Yang33Yang33
提出日時 2018-08-02 17:10:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,571 bytes
コンパイル時間 1,882 ms
コンパイル使用メモリ 170,588 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 21:02:47
合計ジャッジ時間 2,554 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/08/02  Problem: yukicoder 209  / Link: http://yukicoder.me/problems/no/209  ----- */
/* ------問題------

Find the maximum length of the subsequence {Bi}ki=1 of the given sequence {Ai}Ni=1 such that
1≤∃j≤k,B1<B2<⋯<Bj>⋯>Bk−1>Bk∧|B1−B2|<|B2−B3|<⋯<|Bj−1−Bj|∧|Bj−Bj+1|>|Bj+1−Bj+2|>⋯>|Bk−1−Bk|.

-----問題ここまで----- */
/* -----解説等-----

繰り返す部分が多そうにみえる

----解説ここまで---- */

int L[102][102], R[102][102];

// [l,rend]はOKだった
int Lf(int l, int rend, const VI&a) {
	if (L[l][rend] != -1) {
		return L[l][rend];
	}
	int ret = 0;
	FOR(k, 0, l) {
		if ( a[k] < a[l]
			&& abs(a[k]-a[l]) < abs(a[l]-a[rend]) ) {
			ret = max(ret, Lf(k, l, a) + 1);
		}
	}

	return L[l][rend] = ret;

}
// [lbegin,r]はOKだった
int Rf(int lbegin, int r, const VI&a) {
	if (R[lbegin][r] != -1) {
		return R[lbegin][r];
	}
	int ret = 0;
	FOR(k, r + 1, SZ(a)) {
		if (a[r] > a[k]
			&& abs(a[lbegin] - a[r]) > abs(a[r] - a[k])) {
			ret = max(ret, Rf(r,k, a) + 1);
		}
	}

	return R[lbegin][r] = ret;
}


int f(int Top, const VI &a) {
	int Lval = 0, Rval = 0;
	FOR(i, 0, Top) {
		if (a[i] < a[Top])
			Lval = max(Lval, Lf(i, Top, a) + 1);
	}

	FOR(i, Top + 1, SZ(a)) {
		if (a[Top] > a[i])
			Rval = max(Rval, Rf(Top, i, a) + 1);
	}


	return Lval + Rval + 1;
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int T; cin >> T;
	FOR(_, 0, T) {
		int N; cin >> N;
		VI a(N);
		FOR(i, 0, N) {
			cin >> a[i];
		}
		fill(*L, *L + 102 * 102, -1);
		fill(*R, *R + 102 * 102, -1);

		int ans = 0;
		FOR(top, 0, N) {
			ans = max(ans, f(top, a));
		}
		cout << ans << "\n";
	}

	return 0;
}
0