結果
| 問題 | 
                            No.127 門松もどき
                             | 
                    
| コンテスト | |
| ユーザー | 
                             assy1028
                         | 
                    
| 提出日時 | 2016-12-28 15:35:16 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                            (最新)
                                AC
                                 
                             
                            (最初)
                            
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 5,853 bytes | 
| コンパイル時間 | 1,205 ms | 
| コンパイル使用メモリ | 103,756 KB | 
| 実行使用メモリ | 145,792 KB | 
| 最終ジャッジ日時 | 2024-12-21 14:54:01 | 
| 合計ジャッジ時間 | 8,073 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 22 WA * 1 | 
ソースコード
#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;
}
            
            
            
        
            
assy1028