bzoj 2298 [HAOI2011]problem a dp+树状数组
题面
解法
考虑补集转化,我们只要求正确的最大个数即可
显然,一些明显就是错误的东西可以直接排除
对于\((x,y)\)相同的位置一定相等
那么我们就可以把\((x,y)\)相等的并在一类
然后考虑一下\((x,y)\)怎么转化,显然就是那一个数在整个数列中排名对应的区间,为\([x+1,n-y]\)
把区间相等的合并一下,剩下的都是不相等的
然后只要选出尽量多的不相交的区间即可
将左端点排序,然后用树状数组优化一下dp即可
时间复杂度:\(O(n\ log\ n)\)
代码
#include <bits/stdc++.h>
#define N 100010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
x = 0; int f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
int l, r, v;
bool operator < (const Node &a) const {
if (l != a.l) return l < a.l;
return r < a.r;
}
} a[N], b[N];
int n, tot, len, f[N], dp[N];
int lowbit(int x) {return x & -x;}
bool cmp(Node a, Node b) {return a.l != b.l || a.r != b.r;}
int query(int x) {
int ret = 0;
for (int i = x; i; i -= lowbit(i))
chkmax(ret, f[i]);
return ret;
}
void modify(int x, int v) {
for (int i = x; i <= n; i += lowbit(i))
chkmax(f[i], v);
}
int main() {
read(n);
for (int i = 1; i <= n; i++) {
int x, y; read(x), read(y);
if (x + y < n) a[++tot] = (Node) {x + 1, n - y, 0};
}
sort(a + 1, a + tot + 1);
Node las = a[1]; int sum = 1;
for (int i = 2; i <= tot; i++)
if (cmp(a[i], las)) {
b[++len] = las, b[len].v = sum;
las = a[i], sum = 1;
} else sum++;
b[++len] = las; b[len].v = sum; int ans = 0;
for (int i = 1; i <= len; i++) chkmin(b[i].v, b[i].r - b[i].l + 1);
sort(b + 1, b + len + 1);
for (int i = 1; i <= len; i++) {
dp[i] = query(b[i].l - 1) + b[i].v;
modify(b[i].r, dp[i]); chkmax(ans, dp[i]);
}
cout << n - ans << "\n";
return 0;
}