【BZOJ】1109: [POI2007]堆积木Klo

题意

(n(1 le n le 100000))个数放在一排,可以一走一些数(后面的数向前移),要求最大化(a_i=i)的数目。

分析

分析容易得到一个dp方程。

题解

(d(i))表示前(i)个数且第(i)个数放在(a_i)位置的最大答案,则(d(i) = max(d(j)+1) (j < i, a_j < a_i, j - a_j le i-a_i)),发现是一个三维偏序。然而这个三维偏序是可以变成二维偏序的。
由于(j-a_j le i-a_i, a_j < a_i)就可以推出(j < i),所以我们求这个二维偏序集的最大链就行了。

#include <bits/stdc++.h>
using namespace std;
inline int getint() {
	int x=0, c=getchar();
	for(; c<48||c>57; c=getchar());
	for(; c>47&&c<58; x=x*10+c-48, c=getchar());
	return x;
}
int c[200005], mx, n, tot;
void upd(int x, int g) {
	for(; x<=mx; x+=x&-x) {
		c[x]=max(c[x], g);
	}
}
int sum(int x) {
	int y=0;
	for(; x; x-=x&-x) {
		y=max(y, c[x]);
	}
	return y;
}
struct dat {
	int i, a;
}a[100005];
inline bool cmp(const dat &a, const dat &b) {
	return a.i==b.i?a.a<b.a:a.i<b.i;
}
int main() {
	n=getint();
	for(int i=1; i<=n; ++i) {
		int w=getint();
		if(i<w) {
			continue;
		}
		++tot;
		a[tot].i=i-w;
		a[tot].a=w;
		mx=max(mx, w);
	}
	sort(a+1, a+1+tot, cmp);
	mx=min(mx, 2*n);
	int ans=0;
	for(int i=1; i<=tot; ++i) {
		int d=1+sum(a[i].a-1);
		upd(a[i].a, d);
		ans=max(ans, d);
	}
	printf("%d
", ans);
	return 0;
}
原文地址:https://www.cnblogs.com/iwtwiioi/p/4985645.html