結果

問題 No.127 門松もどき
ユーザー codershifthcodershifth
提出日時 2015-11-02 09:46:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 166 ms / 5,000 ms
コード長 3,149 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 150,444 KB
実行使用メモリ 73,656 KB
最終ジャッジ日時 2023-08-25 14:09:50
合計ジャッジ時間 3,832 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 166 ms
73,468 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 10 ms
8,512 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 85 ms
40,384 KB
testcase_13 AC 110 ms
52,860 KB
testcase_14 AC 98 ms
48,952 KB
testcase_15 AC 144 ms
66,340 KB
testcase_16 AC 94 ms
47,244 KB
testcase_17 AC 113 ms
54,328 KB
testcase_18 AC 88 ms
45,208 KB
testcase_19 AC 50 ms
28,992 KB
testcase_20 AC 53 ms
29,824 KB
testcase_21 AC 21 ms
15,168 KB
testcase_22 AC 162 ms
73,656 KB
testcase_23 AC 156 ms
73,524 KB
testcase_24 AC 118 ms
57,092 KB
testcase_25 AC 144 ms
67,000 KB
testcase_26 AC 71 ms
34,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;

class PineDecorationSequenceMock {
public:
    //
    //           7
    //       5
    //            8
    //      4
    //    3    6
    //  2           9
    // 1
    //
    // 大きい物から順に選ぶとして、7,5,8,4,9,1 のような順番に選択するような探索方針は何か?
    // 単純に貪欲法だと 7,6,9 とかになってしまう。
    //
    // すでに A[i(1)] < A[i(2)] < ... < A[i(n)] が選ばれているときに A[i(n+1)] を選ぶ方法を考える。
    // 今 A[i(1)] が最後に選ばれているとするとき
    // i(n) < j なる j のうち A[i1] > A[j] なるものを見つける。
    //
    // この段階での状態は
    // * 最後に選んだのが右か左か dpL
    // * 最後に選んだインデックス l
    // * 次にえらぶインデックスの探索場所 [r+1,N-1] (この例では i(n) <= r。 r が必ず選ばれている必要はない)
    // からなる。
    //
    // j は複数とり得るが、j1 < j2 が条件を満たす j とするき
    // 状態 dpL[l][j1-1] から j2 を選ぶケースは
    // 状態 dpL[l][j2-1] から j2 を選ぶケースに含まれる
    //
    // よって [l,r] の幅を一つづつ大きくしていく幅 DP で更新していけばよい。
    void solve(void) {
            int N;
            cin>>N;
            vector<int> A(N);
            REP(i,N) cin>>A[i];

            // 左端が l のときの [l,r] 区間でできる門松もどきの最大長
            // * r は必ず選択されている必要はなくて、r まで探索済みであるというだけ。
            // よって幅 DP として逐次更新すればよい
            vector<vector<int>> dpL(N,vector<int>(N,0));
            // 右端が r のときの [l,r] 区間でできる門松もどきの最大長
            vector<vector<int>> dpR(N,vector<int>(N,0));

            // 幅 DP
            REP(w,N)
            REP(l,N-w)
            {
                int r = l+w;
                if (l == r)     // 幅 0
                {
                    dpL[l][r] = dpR[l][r] = 1;
                    continue;
                }

                // 幅の結果の引き継ぎ
                dpL[l][r] = max(dpL[l][r], dpL[l][r-1]); // [l,...,r-1],r
                dpR[l][r] = max(dpR[l][r], dpR[l+1][r]); // l,[l+1,...,r]

                if ( A[l] < A[r] )
                    dpL[l][r] = max(dpL[l][r], dpR[l+1][r]+1);
                if ( A[l] > A[r] )
                    dpR[l][r] = max(dpR[l][r], dpL[l][r-1]+1);
            }
            int ret = 0;
            REP(l,N)
            FOR(r,l,N)
                ret = max({ret, dpL[l][r], dpR[l][r]});
            cout<<ret<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new PineDecorationSequenceMock();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0