結果

問題 No.209 Longest Mountain Subsequence
ユーザー nebukuro09nebukuro09
提出日時 2017-05-30 18:56:00
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,481 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 106,172 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-03 13:43:18
合計ジャッジ時間 1,608 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

void solve() {
    auto N = readln.chomp.to!int;
    auto H = readln.split.map!(to!int).array;

    auto dp_left = new Tuple!(int, int)[](N);
    
    foreach (i; 0..N) {
        dp_left[i] = tuple(1, 0);
        foreach (j; 0..i) {
            if (H[i] - H[j] <= dp_left[j][1])
                continue;
            if (dp_left[i][0] < dp_left[j][0] + 1)
                dp_left[i] = tuple(dp_left[j][0] + 1, H[i] - H[j]);
            else if (dp_left[i][0] == dp_left[j][0] + 1)
                dp_left[i] = tuple(dp_left[j][0] + 1, min(H[i] - H[j], dp_left[i][1]));
        }
    }


    auto dp_right = new Tuple!(int, int)[](N);
    
    foreach (i; iota(N-1, -1, -1)) {
        dp_right[i] = tuple(1, 0);
        for (int j = N - 1; j > i; j--) {
            if (H[i] - H[j] <= dp_right[j][1])
                continue;
            if (dp_right[i][0] < dp_right[j][0] + 1)
                dp_right[i] = tuple(dp_right[j][0] + 1, H[i] - H[j]);
            else if (dp_right[i][0] == dp_right[j][0] + 1)
                dp_right[i] = tuple(dp_right[j][0] + 1, min(H[i] - H[j], dp_right[i][1]));
        }
    }


    int ans = 0;
    foreach (i; 0..N) ans = max(ans, dp_left[i][0] + dp_right[i][0] - 1);
    ans.writeln;
}

void main() {
    auto T = readln.chomp.to!int;
    while (T--) solve;
}
0