結果

問題 No.284 門松と魔法(2)
ユーザー snukesnuke
提出日時 2015-09-19 01:03:30
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,902 bytes
コンパイル時間 1,548 ms
コンパイル使用メモリ 102,528 KB
実行使用メモリ 626,460 KB
最終ジャッジ日時 2023-09-26 13:09:18
合計ジャッジ時間 59,597 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 MLE -
testcase_02 MLE -
testcase_03 MLE -
testcase_04 MLE -
testcase_05 MLE -
testcase_06 MLE -
testcase_07 MLE -
testcase_08 MLE -
testcase_09 MLE -
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 MLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 MLE -
testcase_25 MLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 MLE -
testcase_35 MLE -
testcase_36 MLE -
testcase_37 MLE -
testcase_38 MLE -
testcase_39 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rrep(i,n) for(int i = 1; i <= n; ++i)
#define drep(i,n) for(int i = n-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}

const int MX = 100005, INF = 1000010000;
const int MY = 1000005;
const ll LINF = 1000000000000000000ll;
const double eps = 1e-10;

int n;
int ax[MX];

// Segment tree (RMQ type)
struct seg {
  vector<vvi> d; int x2;
  seg(){}
  seg(int mx){
    x2 = 1; while(x2 < mx) x2 <<= 1;
    d.resize(x2<<1,{{0,-10,-10},{0,-10,-10}});
  }
  void add(int i, vi x){
    for(i+=x2;i;i>>=1) {
      if (d[i][0][0] <= x[0]) {
        if (d[i][0][1] != x[1]) d[i][1] = d[i][0];
        d[i][0] = x;
      } else if (d[i][1][0] < x[0] && d[i][1][1] != x[1]) {
        d[i][1] = x;
      }
    }
  }
  vi get(int a, int b, int x, int i=1, int l=0, int r=-10){
    // cout<<a<<" "<<b<<" "<<x<<" "<<i<<" "<<l<<" "<<r<<endl;
    if (a >= b) return {0,-10,-10};
    if (a < 0) return {0,-10,-10};
    if (b > x2) return {0,-10,-10};
    if (r == -10) r = x2;
    if(a <= l && r <= b) {
      if (d[i][0][1] != x) return d[i][0];
      return d[i][1];
    }
    int c = (l+r)>>1; vi res = {0,-10,-10};
    if(a < c) res = max(res,get(a,b,x,i<<1,l,c));
    if(c < b) res = max(res,get(a,b,x,i<<1|1,c,r));
    return res;
  }
};
//

int solve() {
  seg dl(MY), dr(MY);
  rep(i,n) {
    int a = ax[i];
    vi ml = dl.get(0,a,a);
    vi mr = dr.get(a+1,MY,a);
    vi nl = max(dl.get(0,ml[1],a),dl.get(ml[1]+1,a,a));
    vi nr = max(dr.get(a+1,mr[1],a),dr.get(mr[1]+1,MY,a));
    dl.add(a,{mr[0]+1,mr[2],a});
    dr.add(a,{ml[0]+1,ml[2],a});
    dl.add(a,{nr[0]+1,nr[2],a});
    dr.add(a,{nl[0]+1,nl[2],a});
    // cout<<i<<" "<<a<<endl;
    // priv(ml);
    // priv(mr);
    // priv(nl);
    // priv(nr);
  }
  return max(dl.get(0,MY,-10)[0],dr.get(0,MY,-10)[0]);
}

int main() {
  scanf("%d",&n);
  rep(i,n) scanf("%d",&ax[i]);
  int ans = solve();
  if (ans < 3) ans = 0;
  cout<<ans<<endl;
  return 0;
}




0