过了之后感觉曾经真的做过这样的类型的题。

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

对于此题,若按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;
}

HDU 1677 Nested Dolls的更多相关文章

  1. hdu 1677 Nested Dolls【贪心解嵌套娃娃问题】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. HDU 1277 Nested Dolls

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意: 玩俄罗斯套娃,问最后至少还剩几个. 题解: 这题可以和拦截导弹做对比,因为这里是二维的 ...

  3. hdu----(1677)Nested Dolls(DP/LIS(二维))

    Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. HDU 1677

    Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. Nested Dolls 贪心 + dp

    G: Nested Dolls Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 99     Solved: 19 Descript ...

  6. SPOJ 3943 - Nested Dolls 最长不下降子序列LIS(二分写法)

    现在n(<=20000)个俄罗斯套娃,每个都有宽度wi和高度hi(均小于10000),要求w1<w2并且h1<h2的时候才可以合并,问最少能剩几个. [LIS]乍一看跟[这题]类似, ...

  7. 8.3 LIS LCS LCIS(完结了==!)

    感觉这个专题真不好捉,伤心了,慢慢啃吧,孩纸 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28195#overview 密码  ac ...

  8. POJ3636Nested Dolls[DP LIS]

    Nested Dolls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8323   Accepted: 2262 Desc ...

  9. LCS,LIS,LCIS

    网站:CSUST 8月3日(LCS,LIS,LCIS) LCS:      以下讲解来自:http://blog.csdn.net/yysdsyl/article/details/4226630 [问 ...

随机推荐

  1. XML,Object,Json分析转换Xstream采用

    XML,Object,Json转换之浅析Xstream的使用 请尊重他人的劳动成果,转载请注明出处:XML,Object,Json转换之浅析Xstream的使用 XStream的是一个简单的库.主要用 ...

  2. java 调用mysql的存储过程(简单示例)

    首先我在mysql的test数据库里定义了一个student表: create table student4( id   int   primary key, sanme char(5) ); 插入几 ...

  3. linux--shell script

    下面是最近学习shell script的一些知识点总结 ***博客园-邦邦酱好***   1.介绍shell是一个文字接口,让我们与系统沟通.shell script就是针对shell所写的脚本.它就 ...

  4. HashMap源码解读(转)

    http://www.360doc.com/content/10/1214/22/573136_78188909.shtml 最近朋友推荐的一个很好的工作,又是面了2轮没通过,已经是好几次朋友内推没过 ...

  5. android maven eclipse里面新建mavenprojectThe desired archetype does not exist

    这个问题头疼死我了 又一次配置下你看我的教程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmFpbmkxMTk=/font/5a6L5L2T/f ...

  6. java 解析 json 遍历未知key

    1.—————————————————————————————————————————————————————————————— import net.sf.json.JSONObject; Stri ...

  7. 如何让HTML在手机上实现直接拨打电话以及发送短信?

    拨打电话的HTML实现方式: <a href="tel:134289210xx″>拨打电话</a> 上面是比较常用的方式,但是有可能在某些场景下是支持不太好,可以试用 ...

  8. HDU 4998 Rotate

    题意: n次旋转  每次平面绕ai点旋转pi弧度  问  最后状态相当于初始状态绕A点旋转P弧度  A和P是多少 思路: 如果初始X点的最后状态为X'点  则圆心一定在X和X'连线的垂直平分线上  那 ...

  9. OSChina底层数据库操作的类(QueryHelper)源代码

    OSChina 使用的是 dbutils 这个JDBC的封装类库来进行数据库操作. 而 QueryHelper 则是在 dbutils 的基础上进行一级简单的封装,提供一些经常使用的数据库操作方法和对 ...

  10. 基于KMP与Levenshtein模糊匹配算法的银行联行号查询(转)

    在人民银行那里,每个银行的每一个营业网点都有自己唯一的银行联行号,根据这个号码能快速定位一间银行具体的分支行,就像根据一个身份证号码能快速确定一个人一样.例如汇款时,汇款单上要求填写收款人开户行,然后 ...