HDU 1677Nested Dolls

Source: Internet
Author: User

After that, I felt like I had really done this type of problem.

have been very puzzled about the priority of level two ranking, now found that level two sort really does not have absolute priority.

For this problem, if you sort by W, there are 1 to I items W is less than the I+1 item (set to a) of the W, then for the first i+1 we will select a B in [1,i], so that B.W < A.W && B.h < A.h and B.h as large as possible.

This is the so-called B that is closest to a.

Since for W, the back is greater than or equal to the front, so we need a maximum of H.

Splay_tree implementation.

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < cstdio> #include <queue> #include <cmath> #include <stack> #include <string> #include <map > #include <ctime> #pragma comment (linker, "/stack:1024000000"); #define EPS (1e-8) #define LL Long Long#define ULL unsigned long long#define _ll __int64#define INF 0x3f3f3f3f#define Mod 300using namespace std;struct n{//info I    NT Son[2],pre;    data int w,h;    int ls,rs,s;    int minw,minh,maxh;        BOOL operator < (const N &a) const{if (w = = A.W) return h < a.h;    Return W < A.W;    }}st[20010],num[20010];int top;void updata (int root) {st[root].ls = 0,st[root].rs = 0; St[root].    MINW = ST[ROOT].W; St[root].    Minh = st[root].h; St[root].    Maxh = st[root].h;        if (st[root].son[0]! =-1) {st[root].ls = St[st[root].son[0]].s; St[root]. minw = min (St[root]. Minw,st[st[root].son[0]].      MINW);  St[root]. Minh = min (St[root]. Minh,st[st[root].son[0]].        Minh); St[root]. MAXH = Max (St[root]. Maxh,st[st[root].son[0]].    MAXH);        } if (st[root].son[1]! =-1) {st[root].rs = St[st[root].son[1]].s; St[root]. minw = min (St[root]. MINW,ST[ST[ROOT].SON[1]].        MINW); St[root]. Minh = min (St[root]. MINH,ST[ST[ROOT].SON[1]].        Minh); St[root]. MAXH = Max (St[root]. MAXH,ST[ST[ROOT].SON[1]].    MAXH); } ST[ROOT].S = st[root].ls + st[root].rs + 1;} void Push_down (int root) {;}    void Rotate (int root,int dir) {St[st[root].pre].son[dir] = St[root].son[1^dir];    St[root].son[1^dir] = St[root].pre;    if (st[st[st[root].pre].pre].son[0] = = st[root].pre) st[st[st[root].pre].pre].son[0] = root;    else st[st[st[root].pre].pre].son[1] = root;    int temp = St[root].pre;    St[root].pre = St[st[root].pre].pre;    St[temp].pre = root;    if (St[temp].son[dir]! =-1) st[st[temp].son[dir]].pre = temp;    Updata (temp); Updata (root);} int splay (int root,int Goal) {while (St[root].pre! = goal) {Rotate (root, (st[st[root].pre].son[0] = = root? 0:1)); } return root;}    int search_site (int root,int Site) {push_down (root);    int temp;    if (st[root].ls + 1 = = Site) temp = root;    else if (St[root].ls + 1 < site) temp = search_site (st[root].son[1],site-st[root].ls-1);    else temp = Search_site (st[root].son[0],site);    Updata (root); return temp;}    void Init (int l,int r,int &root,int pre) {if (L > R) return;    int mid = (l+r) >>1;    root = top++;    St[root] = Num[mid];    St[root].pre = pre,st[root].son[0] = -1,st[root].son[1] =-1;    Init (L,mid-1,st[root].son[0],root);    Init (Mid+1,r,st[root].son[1],root); Updata (root);}    void Query (int root,int w,int h,int &anw,int &maxh) {if (root = =-1) return;        if (w <= st[root].w) {Query (ST[ROOT].SON[0],W,H,ANW,MAXH);    return; } if (St[root].h < h && st[root].h > Maxh) {       Maxh = st[root].h;    ANW = root; } if (st[root].son[1]! =-1 && st[st[root].son[1]]. MINW < W && St[st[root].son[1]]. Minh < H && St[st[root].son[1]].    Maxh > Maxh) {Query (ST[ROOT].SON[1],W,H,ANW,MAXH); } Query (ST[ROOT].SON[0],W,H,ANW,MAXH);}    int main () {int T;    scanf ("%d", &t);    int n;    int root,i;        while (t--) {scanf ("%d", &n);        root =-1;        Top = 1;        St[0].son[0] = -1,st[0].son[1] =-1;        NUM[1].W =-1;        Num[1].h =-1;        NUM[N+2].W = 1000000000;        Num[n+2].h = 1000000000;        for (i = 2;i <= n+1; ++i) scanf ("%d%d", &num[i].w,&num[i].h);        Sort (num+1,num+n+3);        Init (1,n+2,root,0);        int ans = n;            for (i = 2;i <= n+1; ++i) {root = Splay (Search_site (ROOT,ST[ROOT].S), 0);            Root = Splay (Search_site (root,1), 0);            int ANW = -1,MAXH =-1; Query (St[st[root].son[1]].son[0],NUM[I].W,NUM[I].H,ANW,MAXH);            if (anw = =-1) continue;            ans--;            Root = Splay (anw,0);            Root = Splay (Search_site (root,st[anw].ls+2), 0);            Root = Splay (Search_site (ROOT,ST[ANW].LS), 0);            St[st[root].son[1]].son[0] =-1;            Updata (st[root].son[1]);        Updata (root);    } printf ("%d\n", ans); } return 0;}


HDU 1677Nested Dolls

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.