1. 程式人生 > >bzoj 2298 [HAOI2011]problem a dp+樹狀數組

bzoj 2298 [HAOI2011]problem a dp+樹狀數組

opera sin 補集 its char bool name node c++ namespace

題面

題目傳送門

解法

考慮補集轉化,我們只要求正確的最大個數即可

顯然,一些明顯就是錯誤的東西可以直接排除

對於\((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;
}

bzoj 2298 [HAOI2011]problem a dp+樹狀數組