結果

問題 No.127 門松もどき
コンテスト
ユーザー tottoripaper
提出日時 2015-03-16 19:12:50
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 966 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,626 ms
コンパイル使用メモリ 53,908 KB
実行使用メモリ 74,464 KB
最終ジャッジ日時 2026-03-18 05:26:29
合計ジャッジ時間 4,271 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <cstring>
#include <algorithm>

int N, A[3001];
int dp[2][3001][3001];

int rec(int l, int r, int p){
    if(l == r){return 0;}
    if(~dp[p][l][r]){return dp[p][l][r];}
    
    int res = 0;
    if(p == 0){ // 左はとったぞ
        res = std::max(res, rec(l, r-1, p));
        if(A[l] < A[r]){
            res = std::max({res, 1 + rec(l+1, r, !p)});
        }
    }else{ // 右はとったぞ
        res = std::max(res, rec(l+1, r, p));
        if(A[r] < A[l]){
            res = std::max(res, 1 + rec(l, r-1, !p));
        }
    }

    // printf("%d %d %d: %d\n", l, r, p, res);
    return dp[p][l][r] = res;
}

int main(){
    scanf("%d", &N);

    for(int i=1;i<=N;i++){
        scanf("%d", A+i);
    }

    memset(dp, -1, sizeof(dp));

    int res = 0;
    for(int i=1;i<=N;i++){
        for(int j=i+1;j<=N;j++){
            res = std::max({res, 1 + rec(i, j, 0), 1 + rec(i, j, 1)});
        }
    }

    printf("%d\n", res);
}
0