Alice and Bob
Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Problem descriptionalice 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.
Inputthe 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.
Outputfor each test case, output an answer using one line which contains just one number.
Sample Input
222 31 44 52 332 35 76 84 12 53 4
Sample output
12
Source2012 ACM/ICPC Asia Regional Changchun online
Recommendliuyiding:
Alice and Bob have n rows in their hands, each card has its own length H and width W. When Alice's card length and width are greater than or equal to Bob, let's just say that Alice's card can cover Bob's card and ask Alice how many Bob cards can cover at most. Each card can be used only once.
Solution:
Sort the cards of Alice and Bob in H (or W) Ascending Order, and then pick out the cards from Alice than Bob [I ]. h big card into the Multiset, and then find the first in the set than Bob [I ]. and then release the element in the set and add one to the number. Then, run the next Bob element...
Code:
#include<iostream>#include<cstdio>#include<set>#include<algorithm>using namespace std;const int maxN=110000;int n;struct node{ int h,w; node(int h0=0,int w0=0){ h=h0,w=w0; } friend bool operator <(node a,node b){ if(a.w!=b.w) return a.w<b.w; else return a.h<b.h; }}Alice[maxN],Bob[maxN];bool cmp(node a,node b){ if(a.h!=b.h) return a.h>b.h; else return a.w>b.w;}void solve(){ multiset <node> mul; sort(Alice,Alice+n,cmp); sort(Bob,Bob+n,cmp); int r=0,cnt=0; for(int i=0;i<n;i++){ while(r<n&&Alice[r].h>=Bob[i].h) mul.insert(Alice[r++]); multiset<node>::iterator it=mul.lower_bound(Bob[i]); if(it!=mul.end()){ mul.erase(it); cnt++; } } cout<<cnt<<endl;}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d%d",&Alice[i].h,&Alice[i].w); for(int i=0;i<n;i++) scanf("%d%d",&Bob[i].h,&Bob[i].w); solve(); }}
HDU 4268 Alice and Bob (Greedy)