結果

問題 No.284 門松と魔法(2)
ユーザー 👑 kmjpkmjp
提出日時 2015-09-19 18:39:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,701 ms / 5,000 ms
コード長 3,316 bytes
コンパイル時間 1,918 ms
コンパイル使用メモリ 178,732 KB
実行使用メモリ 21,004 KB
最終ジャッジ日時 2023-09-26 13:53:20
合計ジャッジ時間 20,216 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
15,492 KB
testcase_01 AC 7 ms
15,464 KB
testcase_02 AC 7 ms
15,496 KB
testcase_03 AC 7 ms
15,408 KB
testcase_04 AC 7 ms
15,404 KB
testcase_05 AC 7 ms
15,400 KB
testcase_06 AC 7 ms
15,480 KB
testcase_07 AC 7 ms
15,580 KB
testcase_08 AC 7 ms
15,464 KB
testcase_09 AC 6 ms
15,400 KB
testcase_10 AC 10 ms
15,496 KB
testcase_11 AC 7 ms
15,720 KB
testcase_12 AC 10 ms
15,460 KB
testcase_13 AC 8 ms
15,456 KB
testcase_14 AC 8 ms
15,388 KB
testcase_15 AC 8 ms
15,500 KB
testcase_16 AC 9 ms
15,512 KB
testcase_17 AC 8 ms
15,468 KB
testcase_18 AC 9 ms
15,460 KB
testcase_19 AC 8 ms
15,404 KB
testcase_20 AC 8 ms
15,548 KB
testcase_21 AC 8 ms
15,400 KB
testcase_22 AC 12 ms
15,548 KB
testcase_23 AC 144 ms
15,552 KB
testcase_24 AC 1,793 ms
15,812 KB
testcase_25 AC 699 ms
15,556 KB
testcase_26 AC 7 ms
15,408 KB
testcase_27 AC 7 ms
15,404 KB
testcase_28 AC 7 ms
15,400 KB
testcase_29 AC 2,599 ms
17,784 KB
testcase_30 AC 2,073 ms
17,308 KB
testcase_31 AC 1,195 ms
15,752 KB
testcase_32 AC 2,701 ms
20,776 KB
testcase_33 AC 1,299 ms
21,004 KB
testcase_34 AC 1,595 ms
20,824 KB
testcase_35 AC 6 ms
15,412 KB
testcase_36 AC 6 ms
15,456 KB
testcase_37 AC 1,414 ms
15,656 KB
testcase_38 AC 972 ms
15,812 KB
testcase_39 AC 23 ms
15,552 KB
権限があれば一括ダウンロードができます

ソースコード

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(vector<entry> v,node def) {
	sort(ALL(v));
	reverse(ALL(v));
	for(int i=v.size()-1;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);
	return {v[0],v[1]};
}
node comp(node n1,node n2,node def) {
	vector<entry> v;
	if(n1.first.first>0) v.push_back(n1.first);
	if(n1.second.first>0) v.push_back(n1.second);
	if(n2.first.first>0) v.push_back(n2.first);
	if(n2.second.first>0) v.push_back(n2.second);
	return comp(v,def);
}

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 || x>=y) 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);
	}
};

const int sp=1<<17;
SegTree_1<node,sp> st_inc,st_dec;

int N;
int A[101010],rev[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,rev[x]=r.first;
	
	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);
			int m=r.first.second.second;
			vector<entry> e,e2;
			e.push_back(r.first);
			e.push_back(r.second);
			e.push_back(st_inc.val[m+sp].second);
			
			r=comp(st_inc.getval(A[i]+1,m),st_inc.getval(m+1,N+2),st_inc.def);
			m=r.first.second.second;
			e.push_back(r.first);
			e.push_back(r.second);
			e.push_back(st_inc.val[m+sp].second);
			
			FORR(rr,e) if(A[i]!=rr.second.first) e2.push_back({rr.first+1,{rr.second.second,A[i]}}), ma=max(ma,rr.first+1);
			st_dec.update(A[i],comp(e2,st_dec.def));
		}
		{
			// inc
			node hoge=st_inc.def, hoge2=st_inc.def;
			auto r=st_dec.getval(0,A[i]);
			int m=r.first.second.second;
			vector<entry> e,e2;
			e.push_back(r.first);
			e.push_back(r.second);
			e.push_back(st_dec.val[m+sp].second);
			
			r=comp(st_dec.getval(0,m),st_dec.getval(m+1,A[i]),st_dec.def);
			e.push_back(r.first);
			e.push_back(r.second);
			m=r.first.second.second;
			e.push_back(st_dec.val[m+sp].second);
			
			FORR(rr,e) if(A[i]!=rr.second.first) e2.push_back({rr.first+1,{rr.second.second,A[i]}}), ma=max(ma,rr.first+1);
			st_inc.update(A[i],comp(e2,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