zoj 3511 Cake Robbery(線段樹),zojrobbery

來源:互聯網
上載者:User

zoj 3511 Cake Robbery(線段樹),zojrobbery

題目連結:zoj 3511 Cake Robbery

題目大意:就是有一個N邊形的蛋糕,切M刀,從中挑選一塊邊數最多的,保證沒有兩條邊重疊。

解題思路:有多少個頂點即為有多少條邊,所以直接按照切刀切掉點的個數排序,然後用線段樹維護剩下的還有哪些點。

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 10005;#define lson(x) ((x)<<1)#define rson(x) (((x)<<1)|1)int lc[maxn << 2], rc[maxn << 2], s[maxn << 2];inline void pushdown(int u) {    if (s[u] == 0)        s[lson(u)] = s[rson(u)] = 0;}inline void pushup(int u) {    s[u] = s[lson(u)] + s[rson(u)];}void build (int u, int l, int r) {    lc[u] = l;    rc[u] = r;    if (l == r) {        s[u] = 1;        return;    }    int mid = (l + r) / 2;    build(lson(u), l, mid);    build(rson(u), mid + 1, r);    pushup(u);}void modify (int u, int l, int r) {    if (l > r)        return;    if (l <= lc[u] && rc[u] <= r) {        s[u] = 0;        return;    }    pushdown(u);    int mid = (lc[u] + rc[u]) / 2;    if (l <= mid)        modify(lson(u), l, r);    if (r > mid)        modify(rson(u), l, r);    pushup(u);}int N, M;struct Seg {    int l, r, c;    Seg (int l = 0, int r = 0) {        this->l = l;        this->r = r;        this->c = r - l + 1;    }    friend bool operator < (const Seg& a, const Seg& b) {        return a.c < b.c;    }};vector<Seg> vec;int main () {    while (scanf("%d%d", &N, &M) == 2) {        int l, r, ans = 0;        build(1, 1, N);        vec.clear();        while (M--) {            scanf("%d%d", &l, &r);            if (l > r) swap(l, r);            vec.push_back(Seg(l, r));        }        sort(vec.begin(), vec.end());        for (int i = 0; i < vec.size(); i++) {            int tmp = s[1];            modify(1, vec[i].l + 1, vec[i].r - 1);            ans = max(ans, tmp - s[1] + 2);        }        printf("%d\n", max(ans, s[1]));    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.