結果

問題 No.284 門松と魔法(2)
ユーザー koyumeishikoyumeishi
提出日時 2015-09-25 00:41:20
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,646 bytes
コンパイル時間 1,299 ms
コンパイル使用メモリ 101,964 KB
実行使用メモリ 16,064 KB
最終ジャッジ日時 2023-09-26 14:55:42
合計ジャッジ時間 9,585 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
4,376 KB
testcase_27 WA -
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1,106 ms
9,644 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 1,218 ms
15,772 KB
testcase_33 AC 1,083 ms
15,708 KB
testcase_34 AC 1,119 ms
16,064 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 WA -
testcase_37 AC 174 ms
4,376 KB
testcase_38 AC 131 ms
4,380 KB
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

struct node{
	int cnt;
	int l;
	int m;
	node():cnt(0),l(0),m(0){}
	bool operator < (const node& x) const{
		return cnt < x.cnt;
	}
};

struct val{
	node a;
	node b;
	val operator +(const val& x){
		val ret;
		priority_queue<node, vector<node>, less<node>> pq;
		pq.push(a);
		pq.push(b);
		pq.push(x.a);
		pq.push(x.b);
		ret.a = pq.top();
		pq.pop();
		bool ok = false;
		while(pq.size() && !ok){
			if(pq.top().l != ret.a.l){
				ok = true;
				ret.b = pq.top();
			}else{
				pq.pop();
			}
		}
		return ret;
	}
};

class seg{
public:
	vector<val> v;
	int size;
	seg(int n){
		size = 1;
		while(size<n){
			size <<= 1;
		}
		v.resize(2*size);
	}

	void update(int l, int r, node& x, int lb, int ub, int pos){
		if(ub<=l || r<=lb) return;
		if(l<=lb && ub<=r){
			val tmp;
			tmp.a = x;
			v[pos] = v[pos]+tmp;
			return;
		}
		update(l,r,x, lb,(lb+ub)/2, pos*2+1);
		update(l,r,x, (lb+ub)/2,ub, pos*2+2);
		v[pos] = v[pos*2+1] + v[pos*2+2];
	}
	void update(int l, int r, node& x){
		update(l,r,x,0,size,0);
	}

	val get(int l, int r, int lb, int ub, int pos){
		if(ub<=l || r<=lb) return val();
		if(l<=lb && ub<=r){
			return v[pos];
		}
		val x;
		if(!((lb+ub)/2<=l || r<=lb)) x = get(l,r, lb, (lb+ub)/2, pos*2+1);
		val y;
		if(!(ub<=l || r<=(lb+ub)/2)) y = get(l,r, (lb+ub)/2, ub, pos*2+2);
		return x+y;
	}
	val get(int l, int r){
		return get(l,r,0,size,0);
	}
};

template<class T>
vector<T> compress(vector<T> a, int& size){
	auto b = a;
	sort(b.begin(), b.end());
	b.erase( unique(b.begin(), b.end()), b.end());
	for(int i=0; i<a.size(); i++){
		a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin() +1;
	}
	size = b.size();
	return a;
}

int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	for(int i=0; i<n; i++){
		scanf("%d", &a[i]);
	}
	int m;
	a = compress(a, m);

	seg inc(m+1);
	seg dec(m+1);

	for(int i=0; i<n; i++){
		node s,t;
		{
			val x = inc.get(a[i]+1, m+1);
			s.m = a[i];
			if(x.a.l != a[i]){
				s.cnt = x.a.cnt + 1;
				s.l = x.a.m;
				if(s.l == 0) s.l = m+1;
			}else{
				s.cnt = x.b.cnt + 1;
				s.l = x.b.m;
			}
		}
		{
			val x = dec.get(0,a[i]);
			t.m = a[i];
			if(x.a.l != a[i]){
				t.cnt = x.a.cnt + 1;
				t.l = x.a.m;
			}else{
				t.cnt = x.b.cnt + 1;
				t.l = x.b.m;
			}
		}
		dec.update(a[i],a[i]+1, s);
		inc.update(a[i],a[i]+1, t);
	}

	int ans = 0;
	val x = inc.get(0,m+1);
	val y = dec.get(0,m+1);
	ans = max(x.a.cnt, y.a.cnt);
	if(ans <= 2) ans = 0;
	cout << ans << endl;


	return 0;
}
0