HDU 1677 Nested Dolls

过了之后感觉以前真的做过这种类型的题。

之前一直很疑惑二级排序的优先级问题,现在发现二级排序真的没有绝对的优先级。

对于此题,若按W排序,则有1到i件物品的W均小于等于第i+1件物品(设为A)的W,那么对于第i+1件我们在[1,i]中要选取一个B,使得B.w < A.w && B.h < A.h 且B.h尽可能的大。

这就是所谓的最接近A的B。

因为对于W,后面的均大于等于前面的,所以我们需要一个尽可能大的H。

Splay_Tree实现。

#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 300

using namespace std;

struct N
{
    //info
    int 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值