hdu 4268 Alice and Bob

来源:互联网 发布:菊花插件dbm数据 编辑:程序博客网 时间:2024/05/18 14:12

Alice and Bob

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


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
221 23 42 34 532 35 76 84 12 53 4
 

Sample Output
12
#include<iostream>#include<cstdio>#include<cstring>#include<set>#include<algorithm>using namespace std;#define N 1000000struct node{int w,h,id;bool operator<(const node& x){if(w==x.w){if(h==x.h)return id<x.id;return h<x.h;}return w<x.w;}}a[N*2];struct cmp{bool operator()(const node& x,const node& y){return x.h>y.h;}};multiset<node,cmp>b;multiset<node,cmp>::iterator it;int main(){int n,i,j,T,ans;scanf("%d",&T);while(T--){scanf("%d",&n);b.clear();for(i=0;i<n;i++)scanf("%d%d",&a[i].h,&a[i].w),a[i].id=1;for(i=n;i<2*n;i++)scanf("%d%d",&a[i].h,&a[i].w),a[i].id=0;sort(a,a+2*n);ans=0;for(i=0;i<2*n;i++){if(a[i].id==1){it=b.begin();while(it!=b.end()){if(it->h<=a[i].h){ans++;b.erase(it);break;}it++;}}else b.insert(a[i]);}printf("%d\n",ans);}return 0;}
//Treap平衡树解法#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<ctime>using namespace std;#define N 1000010struct node{    int w,h,id;    bool operator<(const node& x){        if(w==x.w){            if(h==x.h)return id<x.id;            return h<x.h;        }        return w<x.w;    }}a[N*2];/**********************************************/#define nil 0struct Node{    int k,w,l,r;    void init(int v){        k=v;l=r=nil;w=rand();    }}T[N];class Treap{public:    int root,w;    Treap(){root=w=nil;T[0].init(0);T[0].w=0x3fffffff;}    void cut(int x,int &l,int &r,int k){//x 拆分为l,r        if(x==nil){l=r=nil;return;}        if(T[x].k<=k){            l=x;            cut(T[x].r,T[l].r,r,k);        }else{            r=x;            cut(T[x].l,l,T[r].l,k);        }    }    void merge(int &x,int l,int r){//l r合并到x        if(l==nil||r==nil){x=(l==nil?r:l);return;}        if(T[l].w<T[r].w){            merge(T[l].r,T[l].r,r);            x=l;        }else{            merge(T[r].l,l,T[r].l);            x=r;        }    }    void insert(int k){//插入k        int x=++w,l,r;        T[x].init(k);        cut(root,l,r,k);        merge(l,l,x);        merge(root,l,r);    }    bool find(int k){//找到最k的前驱并删除        int x,l,r,f=nil;bool b=false;cut(root,l,r,k);if(l!=nil){b=true;x=l;while(T[x].r!=nil)f=x,x=T[x].r;if(f==nil)merge(l,T[x].l,T[x].r);else merge(T[f].r,T[x].l,T[x].r);//删除x}merge(root,l,r);        return b;    }    void dfs(int x){        if(x==nil)return;        dfs(T[x].l);        cout<<T[x].k<<" ";        dfs(T[x].r);    }};/***********************************************/int main(){    int n,i,j,T,ans;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        for(i=0;i<n;i++)scanf("%d%d",&a[i].h,&a[i].w),a[i].id=1;        for(i=n;i<2*n;i++)scanf("%d%d",&a[i].h,&a[i].w),a[i].id=0;        sort(a,a+2*n);        ans=0;        Treap treap;        /*for(i=0;i<10;i++)treap.insert(i);        treap.find(5);            treap.dfs(treap.root);cout<<endl;*/        for(i=0;i<2*n;i++){            if(a[i].id==1){                if(treap.find(a[i].h))ans++;            }else treap.insert(a[i].h);        }        printf("%d\n",ans);    }return 0;}


原创粉丝点击