結果

問題 No.127 門松もどき
ユーザー assy1028assy1028
提出日時 2016-12-28 15:35:16
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 5,853 bytes
コンパイル時間 1,119 ms
コンパイル使用メモリ 102,872 KB
実行使用メモリ 145,992 KB
最終ジャッジ日時 2023-08-23 12:09:20
合計ジャッジ時間 6,832 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
145,316 KB
testcase_01 AC 39 ms
145,236 KB
testcase_02 AC 38 ms
145,176 KB
testcase_03 AC 39 ms
145,280 KB
testcase_04 AC 372 ms
145,972 KB
testcase_05 AC 38 ms
145,204 KB
testcase_06 WA -
testcase_07 AC 38 ms
145,396 KB
testcase_08 AC 38 ms
145,180 KB
testcase_09 AC 40 ms
145,192 KB
testcase_10 AC 38 ms
145,216 KB
testcase_11 AC 38 ms
145,232 KB
testcase_12 AC 148 ms
145,664 KB
testcase_13 AC 185 ms
145,784 KB
testcase_14 AC 185 ms
145,656 KB
testcase_15 AC 247 ms
145,912 KB
testcase_16 AC 163 ms
145,716 KB
testcase_17 AC 113 ms
145,736 KB
testcase_18 AC 112 ms
145,684 KB
testcase_19 AC 76 ms
145,536 KB
testcase_20 AC 67 ms
145,544 KB
testcase_21 AC 53 ms
145,492 KB
testcase_22 AC 336 ms
145,992 KB
testcase_23 AC 219 ms
145,824 KB
testcase_24 AC 280 ms
145,908 KB
testcase_25 AC 338 ms
145,768 KB
testcase_26 AC 169 ms
145,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
        size_t seed = hash<T>()(x.first);
        return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n;
int a[3010];

vector<int> l[2][3010];
vector<int> r[2][3010];

int dp[2][3010][3010][2];

int calc(int s, int t, int b, int c) {
    if (dp[b][s][t][c] == -1) {
        if (s == t) {
            int res = 1;
            if (l[b][s].size() > 0 || r[b][s].size() > 0) res = 2;
            for (int i = 0; i < l[b][s].size(); i++) {
                for (int j = 0; j < r[b][s].size(); j++) {
                    res = max(res, 3 + calc(l[b][s][i], r[b][s][j], b, c));
                    /*
                                          if (a[l[b][s][i]] > a[r[b][s][j]]) {
                                                                  res = max(res, 3 + calc(l[b][s][i], r[b][s][j], b, 0));
                                                                                      } else if (a[l[b][s][i]] < a[r[b][s][j]]) {
                                                                                                              res = max(res, 3 + calc(l[b][s][i], r[b][s][j], b, 1));
                                                                                                                                  } else {
                                                                                                                                                          res = max(res, 3 + calc(l[b][s][i], r[b][s][j], b, 0));
                                                                                                                                                                                  res = max(res, 3 + calc(l[b][s][i], r[b][s][j], b, 1));
                                                                                                                                                                                                      }
                                                                                                                                                                                                                          */
                }
            }
            dp[b][s][t][c] = res;
        } else {
            int res = 0;
            if ((c == 0 && b == 0 && a[s] <= a[t]) || (c == 1 && b == 1 && a[s] >= a[t])) {
                for (int i = 0; i < r[b][t].size(); i++) {
                    res = max(res, calc(s, r[b][t][i], b, c));
                }
                if (res == 0) res = -inf;
            } else if ((c == 1 && b == 0 && a[s] >= a[t]) || (c == 0 && b == 1 && a[s] <= a[t])) {
                for (int i = 0; i < l[b][s].size(); i++) {
                    res = max(res, calc(l[b][s][i], t, b, c));
                }
                if (res == 0) res = -inf;
            } else if ((b == 0 && c == 1) || (b == 1 && c == 0)) {
                for (int i = 0; i < r[b][t].size(); i++) {
                    res = max(res, 1 + calc(s, r[b][t][i], b, 1-c));
                    /*
                                          if ((b == 0 && a[r[b][t][i]] < a[s] ) || (b == 1 && a[r[b][t][i]] > a[s]))

                    */
                }
            } else {
                for (int i = 0; i < l[b][s].size(); i++) {
                    res = max(res, 1 + calc(l[b][s][i], t, b, 1-c));
                    /*
                                          if ((b == 0 && a[l[b][s][i]] < a[t]) || (b == 1 && a[l[b][s][i]] > a[t]))
                                                                  res = max(res, 1 + calc(l[b][s][i], t, b));
                    */
                }
            }
            dp[b][s][t][c] = res;
        }
    }
    return dp[b][s][t][c];
}

int solve() {
    for (int i = 0; i < n; i++) {
        for (int j = i-1; j >= 0; j--) {
            if (a[j] < a[i] && (l[0][i].empty() || (!l[0][i].empty() && a[l[0][i].back()] < a[j]))) {
                l[0][i].push_back(j);
            }
            if (a[j] > a[i] && (l[1][i].empty() || (!l[1][i].empty() && a[l[1][i].back()] > a[j]))) {
                l[1][i].push_back(j);
            }
        }
        for (int j = i+1; j < n; j++) {
            if (a[j] < a[i] && (r[0][i].empty() || (!r[0][i].empty() && a[r[0][i].back()] < a[j]))) {
                r[0][i].push_back(j);
            }
            if (a[j] > a[i] && (r[1][i].empty() || (!r[1][i].empty() && a[r[1][i].back()] > a[j]))) {
                r[1][i].push_back(j);
            }
        }
    }

    memset(dp, -1, sizeof(dp));
    int res = 0;
    for (int i = 0; i < n; i++) {
        res = max(res, max(calc(i, i, 0, 0), calc(i, i, 0, 1)));
        //res = max(res, max(calc(i, i, 1, 0), calc(i, i, 1, 1)));
    }
    return res;
}

void input() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}
0