【CodeVS 3289】【NOIP 2013】花匠

http://codevs.cn/problem/3289/

dp转移,树状数组维护前缀max和后缀max进行优化,$O(nlogn)$。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100003;
int in() {
	int k = 0, fh = 1; char c = getchar();
	for(; c < '0' || c > '9'; c = getchar())
		if (c == '-') fh = -1;
	for(; c >= '0' && c <= '9'; c = getchar())
		k = (k << 3) + (k << 1) + c - '0';
	return k * fh;
}

int n, h[N], H[N], cnt, bit1[N], bit2[N];

void update1(int pos, int x) {
	for(; pos; pos -= (pos & (-pos)))
		bit1[pos] = max(bit1[pos], x);
}
int Max1(int pos) {
	int ret = 0;
	for(; pos < cnt; pos += (pos & (-pos)))
		ret = max(ret, bit1[pos]);
	return ret;
}

void update2(int pos, int x) {
	for(; pos < cnt; pos += (pos & (-pos)))
		bit2[pos] = max(bit2[pos], x);
}
int Max2(int pos) {
	int ret = 0;
	for(; pos; pos -= (pos & (-pos)))
		ret = max(ret, bit2[pos]);
	return ret;
}

int ans = 0, f;

int main() {
	n = in(); cnt = n;
	for(int i = 1; i <= n; ++i) {
		h[i] = in();
		H[i] = h[i];
	}
	sort(H + 1, H + n + 1);
	cnt = unique(H + 1, H + cnt + 1) - H;
	for(int i = 1; i <= n; ++i)
		h[i] = lower_bound(H + 1, H + cnt, h[i]) - H;
	
	update1(h[1], 1); update2(h[1], 1);
	for(int i = 2; i <= n; ++i) {
		f = Max1(h[i] + 1);
		update2(h[i], f + 1);
		ans = max(ans, f);
		f = Max2(h[i] - 1);
		update1(h[i], f + 1);
		ans = max(ans, f);
	}
	
	printf("%d\n", ans + 1);
	return 0;
}

QwQ

posted @ 2016-08-28 08:01  abclzr  阅读(179)  评论(0编辑  收藏  举报