結果

問題 No.284 門松と魔法(2)
ユーザー pekempeypekempey
提出日時 2015-11-29 02:37:43
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,647 bytes
コンパイル時間 1,644 ms
コンパイル使用メモリ 168,908 KB
実行使用メモリ 12,200 KB
最終ジャッジ日時 2023-10-12 05:36:58
合計ジャッジ時間 5,761 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,032 KB
testcase_01 AC 5 ms
11,008 KB
testcase_02 AC 6 ms
11,452 KB
testcase_03 AC 7 ms
11,320 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 6 ms
11,472 KB
testcase_10 AC 7 ms
11,340 KB
testcase_11 AC 6 ms
11,300 KB
testcase_12 AC 6 ms
11,296 KB
testcase_13 AC 6 ms
11,408 KB
testcase_14 WA -
testcase_15 AC 6 ms
11,316 KB
testcase_16 AC 6 ms
11,348 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 6 ms
11,416 KB
testcase_20 AC 6 ms
11,404 KB
testcase_21 AC 6 ms
11,344 KB
testcase_22 AC 7 ms
11,400 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 6 ms
11,008 KB
testcase_28 WA -
testcase_29 AC 274 ms
12,176 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 294 ms
12,168 KB
testcase_33 AC 196 ms
12,196 KB
testcase_34 AC 194 ms
11,828 KB
testcase_35 AC 6 ms
11,412 KB
testcase_36 AC 6 ms
11,004 KB
testcase_37 AC 160 ms
12,200 KB
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

struct SegmentTree {
	typedef pair<pair<int, int>, pair<int, int>> P; // (max1, l1, m1), (max2, m2, m2)
	vector<P> mx;
	int size;
	P e;
	SegmentTree(int n) {
		size = 1;
		e = {{0, -1}, {0, -1}};
		while (size < n) size *= 2;
		mx.resize(size * 2, e);
	}

	P merge(P a, P b) {
		pair<int, int> x[4] = {a.first, a.second, b.first, b.second};
		sort(x, x + 4);
		return P(x[3], x[2]);
	}

	void update(int k, int p, int v) {
		k += size - 1;
		mx[k] = merge(mx[k], {{v, p}, {-1, -1}});
		while (k > 0) {
			k = (k - 1) / 2;
			mx[k] = merge(mx[k * 2 + 1], mx[k * 2 + 2]);
		}
	}

	P query(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l) return e;
		if (a <= l && r <= b) return mx[k];
		auto x = query(a, b, k * 2 + 1, l, (l + r) / 2);
		auto y = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return merge(x, y);
	}

	P query(int a, int b) {
		return query(a, b, 0, 0, size);
	}
};

int main() {
	int n;
	cin >> n;
	vector<int> a(n);
	rep (i, n) cin >> a[i];
	vector<int> ca(a);
	sort(ca.begin(), ca.end());
	ca.erase(unique(ca.begin(), ca.end()), ca.end());

	rep (i, n) a[i] = lower_bound(ca.begin(), ca.end(), a[i]) - ca.begin();
	SegmentTree inctr(101010), dectr(101010);

	rep (i, n) {
		int r = a[i];
		// dpinc[m][r] = max(dpdec[m+1..][..r-1])
		// =>
		// dpinc[m][r] = max(dpdec[..][..r-1])
		// dpinc[..r-1][r] = max(dpdec[..][..r-1])
		// dp[..r-1][r] = query: [..r-1] => (max,l)
		auto maxdec = dectr.query(0, r);
		if (maxdec.first.second != r) {
			inctr.update(r, r, maxdec.first.first + 1);
		} else {
			inctr.update(r, r, maxdec.second.first + 1);
		}
		// dpdec[m][r] = max(dpinc[..m-1][r+1..])
		// =>
		// dpdec[m][r] = max(dpinc[..][r+1..])
		// dpdec[r+1..][r] = max(dpinc[..][r+1..])
		// dp[r+1..][r] = query: [..r+1] => (max,l)
		auto maxinc = inctr.query(r + 1, inctr.size);
		if (maxinc.first.second != r) {
			dectr.update(r, r, maxinc.first.first + 1);
		} else {
			dectr.update(r, r, maxinc.second.first + 1);
		}
	}

	int ans = 0;
	ans = max(ans, inctr.query(0, inctr.size).first.first);
	ans = max(ans, dectr.query(0, dectr.size).first.first);
	cout << ans << endl;
	return 0;
}
0