hdu 4268 Alice and Bob (一)

2014-11-24 02:03:01 · 作者: · 浏览: 5

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
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[cpp]
#include #include #include #include #include using namespace std; #define N 1000000 struct node{ int w,h,id; bool operator<(const node& x){ if(w==x.w){ if(h==x.h)return idy.h; } }; multisetb; multiset::iterator it; int main(){ int n,i,j,T,ans; scanf("%d",&T); while(T--){ scanf("%d",&n); b.clear(); for(i=0;ih<=a[i].h){ ans++; b.erase(it); break; } it++; } }else b.insert(a[i]); } printf("%d\n",ans); } return 0; } #include
#include
#include
#include
#include
using namespace std;
#define N 1000000
struct node{
int w,h,id;
bool operator<(const node& x){
if(w==x.w){
if(h==x.h)return id return h }
return w }
}a[N*2];
struct cmp{
bool operator()(const node& x,const node& y){
return x.h>y.h;
}
};
multisetb;
multiset::iterator it;
int main(){
int n,i,j,T,ans;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
b.clear();
for(i=0;i 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;
}
[cpp]
//Treap平衡树解法 #include #include #include #include #include #include using namespace std; #define N 1000010 struct node{ int w,h,id; bool operator<(const node& x){ if(w==x.w){ if(h==x.h)return id #include
#include
#include
#include
#include
#include
using namespace std;
#define N 1000010
struct node{
int w,h,id;
bool operator<(const node& x){
if(w==x.w){
if(h==x.h)return id return h }
return w }
}a[N*2];
/**********************************************/
#define nil 0
struct 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 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=fals