hdu 4268 Alice and Bob(multiset|段树)

hdu 4268 Alice and Bob(multiset|段树)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Alice and Bob

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2901    Accepted Submission(s): 941

Problem Description
Alice and Bob’s game never ends. Today, they introduce a new game. In this game, both of them have N different rectangular cards respectively. Alice wants to use his cards to cover Bob’s. The card A can cover the card B if the height of A is not smaller than B and the width of A is not smaller than B. As the best programmer, you are asked to compute the maximal number of Bob’s cards that Alice can cover.

Please pay attention that each card can be used only once and the cards cannot be rotated.

 

Input
The first line of the input is a number T (T <= 40) which means the number of test cases.

For each case, the first line is a number N which means the number of cards that Alice and Bob have respectively. Each of the following N (N <= 100,000) lines contains two integers h (h <= 1,000,000,000) and w (w <= 1,000,000,000) which means the height and width of Alice’s card, then the following N lines means that of Bob’s.

 

Output
For each test case, output an answer using one line which contains just one number.

 

Sample Input
   
   
2 2 1 2 3 4 2 3 4 5 3 2 3 5 7 6 8 4 1 2 5 3 4

 

Sample Output
   
   
1 2

 

Source

Recommend
 
题意:
A,B两个人各有n个矩形。

(n<=1e5)如今A想用自己的矩形覆盖B的矩形。一个矩形覆盖还有一个矩形的条件是宽和高都不还有一个矩形相应的宽和高小。如今问A最多能覆盖B的多少个矩形。每一个矩形最多覆盖一个还有一个矩形。且矩形不能旋转。

思路:
就是贪心的思想。先把两个人的矩形都按w排序。

然后对于A的每个矩形。找一个w<=自己w.h<自己H且尽量接近的B的矩形覆盖即可了。

覆盖后删除这两个矩形。对于找上述条件的矩形能够用multiset维护。

可是比赛时换了种姿势用线段树写了发。

思路都差点儿相同。离散化。维护区间和。找小于等于p且最靠右的位置。

具体见代码:

#include <iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define lson L,mid,ls
#define rson mid+1,R,rs
const int maxn=100010;
int num[maxn<<3];//离散化后有2*n忘了改大空间了。

坑struct node{ int w,h;} ag[maxn],bg[maxn];int H[maxn<<1];inline bool cmp(const node &a,const node &b){ if(a.w==b.w) return a.h<b.h; return a.w<b.w;}void build(int L,int R,int rt){ num[rt]=0; if(L==R) return ; int ls=rt<<1,rs=ls|1,mid=(L+R)>>1; build(lson); build(rson);}void update(int L,int R,int rt,int p,int d){ if(L==R) { num[rt]+=d; return; } int ls=rt<<1,rs=ls|1,mid=(L+R)>>1; if(p<=mid) update(lson,p,d); else update(rson,p,d); num[rt]=num[ls]+num[rs];}int qu(int L,int R,int rt,int p){ if(!num[rt]) return -1; if(L==R) return L; int ls=rt<<1,rs=ls|1,mid=(L+R)>>1,pos=-1; if(p<=mid) return qu(lson,p); if(num[rs]) pos=qu(rson,p); if(pos!=-1) return pos; return qu(lson,p);}int main(){ int t,n,i,m,p,ans; scanf("%d",&t); while(t--) { m=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d",&ag[i].h,&ag[i].w); H[m++]=ag[i].h; } for(i=0;i<n;i++) { scanf("%d%d",&bg[i].h,&bg[i].w); H[m++]=bg[i].h; } sort(ag,ag+n,cmp); sort(bg,bg+n,cmp); sort(H,H+m); m=unique(H,H+m)-H; build(1,m,1); ans=p=0; for(i=0;i<n;i++) { while(p<n&&bg[p].w<=ag[i].w) update(1,m,1,lower_bound(H,H+m,bg[p].h)-H+1,1),p++; int pos=qu(1,m,1,lower_bound(H,H+m,ag[i].h)-H+1); if(pos!=-1) { ans++; update(1,m,1,pos,-1); } } printf("%d\n",ans); } return 0;}

版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116762.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • win7 powershell版本过低问题

    win7 powershell版本过低问题

    2021年10月28日
    58
  • 【CAS】CAS原理「建议收藏」

    【CAS】CAS原理「建议收藏」CAS是所有原子类的底层原理,乐观锁主要采用CAS算法。CAS,比较并交换,是==JDK提供的非阻塞原子性操作,通过硬件保证比较-更新操作的原子性。==通常结合volatile保证共享变量的原子性。

    2022年10月16日
    4
  • 机器学习之朴素贝叶斯分类算法

    机器学习之朴素贝叶斯分类算法一、数学知识相关1.独立事件–前提2.条件概率3.全概率公式4.贝叶斯公式5.朴素贝叶斯公式其中:P(A)叫做A事件的先验概率,即一般情况下,认为A发生的概率。 P(B|A)叫做似然度,是A假设条件成立的情况下发生B的概率。 P(A|B)叫做后验概率,在B发生的情况下发生A的概率,也就是要求的概率。P(B)叫做标准化常量,即在一般情况下,认为B…

    2022年10月15日
    1
  • 大数据应用实例[通俗易懂]

    大数据应用实例[通俗易懂]通过对全国各地不同时间段的购买华硕笔记本电脑的销售量进行大数据处理,将其通过汇总分析,即可制成图表的格式,使用户能对华硕笔记本销售量进行研究,可以查看出华硕笔记本最近的销售同比(与上一年的销售比较)与环比(与上月的销售比较),购买的人数地域分布量,及购买的人群年龄分布,性别分布等等首先就是对华硕笔记本最近的销售同比(与上一年的销售比较)与环比(与上月的销售比较),整体同比及移…

    2022年6月2日
    50
  • java开发中什么是bd设计,西安交通大学17年9月课程考试《Java语言程序设计》作业考核试题…

    java开发中什么是bd设计,西安交通大学17年9月课程考试《Java语言程序设计》作业考核试题…西安交通大学17年9月课程考试《Java语言程序设计》作业考核试题试卷总分:100得分:0一、单选题(共25道试题,共50分)1.设x为float型变量,y为double型变量,a为int型变量,b为long型变量,c为char型变量,则表达式x+y*a/x+b/y+c的值为()类型。A.intB.longC.doubleD.char满分:2分2.在Java中用什么关键…

    2022年7月8日
    24
  • 微信模拟位置想在哪就在哪(GPS欺骗)

    微信模拟位置想在哪就在哪(GPS欺骗)微信模拟位置是通过位置欺骗,让微信误以为我们就在当前的位置进行周边好友的搜索与添加。这是一个很老的功能,为什么说很老呢?因为这是在android4.1的时候流行的一个功能,android在4.4推出后这个功能基本不能使用了!即使很老但是最近也碰到了这样的需求,不过针对特殊的群体,手机定制而来,就没有限制了!首先我们要知道怎么用:首先:我们从设置里找到开发者选项(开发者都知道):在这里就是,开发人

    2022年4月30日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号