hdu 4268 Alice and Bob

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4268

题目大意:

Alice和Bob各有N(N<=100,000)张卡,卡有两个属性(w,h),如果A卡的这两个属性均不小于B卡的,则A卡可以覆盖B卡,求Alice最多可以盖Bob几张卡.

题目思路:
如果Alice的某张卡的h大于Bob的某些卡的h,那么可以知道Alice的这张卡应该盖这些卡中w值最大的且w值小于Alice的这张卡的w值(设为W).
假设Alice不盖可以盖的最大w值的那张卡,盖了一张小的(设为W‘),那么如果Alice有一张卡属性为h'和w'(h'>h,W'<w'<W),那么容易发现,如果之前盖了W,那么就可以多盖一张卡了.
通俗的来说就是要盖就盖能盖的最大卡.

代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ull unsigned __int64
#define ll __int64
//#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle l+r>>1
#define INF 0x3F3F3F3F
#define esp (1e-10)
#define MOD 1000000007
#define type int
//const double pi=acos(-1.0);
const int M=100000+5;
#define clr(x,c) memset(x,c,sizeof(x))
type min(type x,type y){return x<y? x:y;}
type max(type x,type y){return x>y? x:y;}
void swap(type& x,type& y){type t=x;x=y;y=t;}
int T,cas=0;

int n,m,ww[M<<1];

struct node{
    int h,w;
    void read(){
        scanf("%d%d",&h,&w);
        ww[++m]=w;
    }
    bool operator < (const node &p) const {
        return h==p.h? w<p.w:h<p.h;
    }
};

node a[M],b[M];

int bs(int key){
    int l=1,r=m,mid;
    while(l<=r){
        mid=middle;
        if(key>ww[mid]) l=mid+1;
        else if(key<ww[mid]) r=mid-1;
        else return mid;
    }return -1;
}

int mmax[M<<3],cnt[M<<1];

void update(int l,int r,int rt,int p,int c){
    if(l==r){
        mmax[rt]= (cnt[l]+=c)? l:0;
        return;
    }
    int mid=middle;
    if(p<=mid) update(lson,p,c);
    else update(rson,p,c);
    mmax[rt]=max(mmax[ls],mmax[rs]);
}

int query(int l,int r,int rt,int L,int R){
    if(L<=l && r<=R) return mmax[rt];
    int mid=middle,ret=0;
    if(R<=mid) return query(lson,L,R);
    else if(mid<L) return query(rson,L,R);
    else return max(query(lson,L,mid),query(rson,mid+1,R));
}

void run(){
    int i,j,k,ret=0;
    scanf("%d",&n),m=0;
    for(i=1;i<=n;i++) a[i].read();
    for(i=1;i<=n;i++) b[i].read();
    sort(ww+1,ww+m+1);
    //m=unique(ww+1,ww+m+1)-(ww+1);
    for(i=2,k=1;i<=m;i++) if(ww[i]!=ww[i-1]) ww[++k]=ww[i];
    m=k;
    sort(a+1,a+n+1),sort(b+1,b+n+1);
    clr(mmax,0),clr(cnt,0);
    for(i=j=1;i<=n;i++){
        while(j<=n && b[j].h<=a[i].h){
            update(1,m,1,bs(b[j].w),1);
            j++;
        }
        k=query(1,m,1,0,bs(a[i].w));
        if(k>0) ret++,update(1,m,1,k,-1);
    }
    printf("%d\n",ret);
}

int main(){
    //freopen("1.in","r",stdin);
    //freopen("1.out","w",stdout);
    //preSof();
    //run();
    //while(~scanf("%d",&n)) run();
    for(scanf("%d",&T),cas=1;cas<=T;cas++) run();
    //system("pause");
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值