結果

問題 No.284 門松と魔法(2)
ユーザー 👑 kmjpkmjp
提出日時 2015-09-19 16:46:26
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 4,260 bytes
コンパイル時間 1,932 ms
コンパイル使用メモリ 179,256 KB
実行使用メモリ 32,872 KB
最終ジャッジ日時 2023-09-26 13:41:53
合計ジャッジ時間 25,209 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 11 ms
27,668 KB
testcase_04 AC 10 ms
27,728 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 10 ms
27,792 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 11 ms
27,664 KB
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 AC 11 ms
27,848 KB
testcase_27 AC 10 ms
27,668 KB
testcase_28 AC 10 ms
27,660 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 2,117 ms
28,204 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 10 ms
27,792 KB
testcase_36 WA -
testcase_37 AC 2,191 ms
27,920 KB
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

typedef pair<int,pair<int,int>> entry;
typedef pair<entry,entry> node;

node comp(node n1,node n2,node def) {
	int i,x=0,y=0;
	node r;
	
	vector<entry> v;
	v.push_back(n1.first);
	v.push_back(n2.first);
	v.push_back(n1.second);
	v.push_back(n2.second);
	sort(ALL(v));
	reverse(ALL(v));
	for(i=4;i>=1;i--) if(v[i].second.first==v[i-1].second.first) v.erase(v.begin()+i);
	while(v.size()<2) v.push_back(def.first);
	
	r = {v[0],v[1]};
	return r;
}

template<class V,int NV> class SegTree_1 {
public:
	vector<V> val;
	V def;
	
	void init(V def_){def=def_;val=vector<V>(NV*2,def);};
	V getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l) return def;
		if(x<=l && r<=y) return val[k];
		return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1),def);
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry]=comp(val[entry],v,def);
		while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1],def);
	}
};

SegTree_1<node,1<<18> st_inc,st_dec;

int N;
int A[101010];
map<int,int> M;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	
	cin>>N;
	FOR(i,N) cin>>A[i],M[A[i]]=0;
	x=0;
	FORR(r,M) r.second=++x;
	
	int ma=0;
	st_inc.init({{0,{0,x+1}},{0,{0,x+1}}});
	st_dec.init({{0,{0,0}},{0,{0,0}}});
	
	FOR(i,N) {
		A[i]=M[A[i]];
		
		{
			// dec
			node hoge=st_dec.def, hoge2=st_dec.def;
			auto r=st_inc.getval(A[i]+1,N+2);
			{
				auto f=r.first;
				auto s=r.second;
				if(A[i]<f.second.second && A[i]!=f.second.first) hoge.first = {f.first+1,{f.second.second,A[i]}}, ma=max(ma,f.first+1);
				if(A[i]<s.second.second && A[i]!=s.second.first) hoge.second = {s.first+1,{s.second.second,A[i]}}, ma=max(ma,s.first+1);
			}
			int m=r.first.second.second;
			r=comp(st_inc.getval(A[i]+1,m),st_inc.getval(m+1,N+2),st_inc.def);
			{
				auto f=r.first;
				auto s=r.second;
				if(A[i]<f.second.second && A[i]!=f.second.first) hoge2.first = {f.first+1,{f.second.second,A[i]}}, ma=max(ma,f.first+1);
				if(A[i]<s.second.second && A[i]!=s.second.first) hoge2.second = {s.first+1,{s.second.second,A[i]}}, ma=max(ma,s.first+1);
			}
			/*
			_P("dec:%d (%d,%d,%d),(%d,%d,%d) -> ",A[i],f.first,f.second.first,f.second.second,s.first,s.second.first,s.second.second);
			_P("(%d,%d,%d),(%d,%d,%d)\n",hoge.first.first,hoge.first.second.first,hoge.first.second.second,
			hoge.second.first,hoge.second.second.first,hoge.second.second.second);*/
			st_dec.update(A[i],comp(hoge,hoge2,st_dec.def));
		}
		{
			// inc
			node hoge=st_inc.def, hoge2=st_inc.def;
			auto r=st_dec.getval(0,A[i]);
			{
				auto f=r.first;
				auto s=r.second;
				if(A[i]>f.second.second && A[i]!=f.second.first) hoge.first = {f.first+1,{f.second.second,A[i]}}, ma=max(ma,f.first+1);
				if(A[i]>s.second.second && A[i]!=s.second.first) hoge.second = {s.first+1,{s.second.second,A[i]}}, ma=max(ma,s.first+1);
			}
			int m=r.first.second.second;
			r=comp(st_dec.getval(0,m),st_inc.getval(m+1,A[i]),st_dec.def);
			{
				auto f=r.first;
				auto s=r.second;
				if(A[i]>f.second.second && A[i]!=f.second.first) hoge2.first = {f.first+1,{f.second.second,A[i]}}, ma=max(ma,f.first+1);
				if(A[i]>s.second.second && A[i]!=s.second.first) hoge2.second = {s.first+1,{s.second.second,A[i]}}, ma=max(ma,s.first+1);
			}
			/*
			_P("inc:%d (%d,%d,%d),(%d,%d,%d) -> ",A[i],f.first,f.second.first,f.second.second,s.first,s.second.first,s.second.second);
			_P("(%d,%d,%d),(%d,%d,%d)\n",hoge.first.first,hoge.first.second.first,hoge.first.second.second,
			hoge.second.first,hoge.second.second.first,hoge.second.second.second);*/
			st_inc.update(A[i],comp(hoge,hoge2,st_inc.def));
		}
	}
	if(ma<3) ma=0;
	cout<<ma<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0