結果
| 問題 |
No.209 Longest Mountain Subsequence
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-16 13:53:28 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 154 ms / 2,000 ms |
| コード長 | 1,210 bytes |
| コンパイル時間 | 842 ms |
| コンパイル使用メモリ | 120,008 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-12 04:28:13 |
| 合計ジャッジ時間 | 2,048 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 6 |
ソースコード
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;
void main()
{
auto t = readln.chomp.to!size_t;
foreach (_; 0..t) {
auto n = readln.chomp.to!size_t;
auto ai = readln.split.map!(to!int).array;
writeln(calc(n, ai));
}
}
int calc(size_t n, int[] ai)
{
auto memo = new int[][](n, n);
int dp(size_t i1, size_t i2 = size_t.max) {
auto maxR = 1;
if (i2 == size_t.max) {
foreach (j; i1+1..n)
maxR = max(maxR, dp(j, i1) + 1);
} else {
if (memo[i1][i2] > 0) {
maxR = memo[i1][i2];
} else {
auto d = ai[i1] - ai[i2];
if (d > 0) {
foreach (j; i1+1..n)
if (ai[j] - ai[i1] > d || ai[j] - ai[i1] < 0)
maxR = max(maxR, dp(j, i1) + 1);
} else {
foreach (j; i1+1..n)
if (ai[j] - ai[i1] > d && ai[j] - ai[i1] < 0)
maxR = max(maxR, dp(j, i1) + 1);
}
memo[i1][i2] = maxR;
}
}
return maxR;
}
auto maxR = 0;
foreach (i; 0..n)
maxR = max(maxR, dp(i));
return maxR;
}