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

來源:互聯網
上載者:User

標籤:include   pac   ref   個數   www   math   git   c++   define   

題面

題目傳送門

解法

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

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

對於\((x,y)\)相同的位置一定相等

那麼我們就可以把\((x,y)\)相等的並在一類

然後考慮一下\((x,y)\)怎麼轉化,顯然就是那一個數在整個數列中排名對應的區間,為\([x+1,n-y]\)

把區間相等的合并一下,剩下的都是不相等的

然後只要選出盡量多的不相交的區間即可

將左端點排序,然後用樹狀數組最佳化一下dp即可

時間複雜度:\(O(n\ log\ n)\)

代碼
#include <bits/stdc++.h>#define N 100010using 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+樹狀數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.