結果

問題 No.127 門松もどき
ユーザー assy1028assy1028
提出日時 2016-12-27 14:13:09
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,412 bytes
コンパイル時間 947 ms
コンパイル使用メモリ 101,204 KB
実行使用メモリ 75,448 KB
最終ジャッジ日時 2023-08-21 13:57:22
合計ジャッジ時間 3,319 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
74,484 KB
testcase_01 AC 20 ms
74,440 KB
testcase_02 AC 21 ms
74,428 KB
testcase_03 AC 21 ms
74,636 KB
testcase_04 WA -
testcase_05 AC 21 ms
74,484 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 21 ms
74,676 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

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];

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

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