Alice and Bob
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5010 Accepted Submission(s): 1577
Total Submission(s): 5010 Accepted Submission(s): 1577
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.
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.
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题意: 爱丽丝和鲍勃的游戏永远不会结束。今天,他们推出了一款新游戏。在这个游戏中,它们都有N个不同的矩形卡。爱丽丝想用他的卡片盖住鲍勃的。如果A的高度不小于B并且A的宽度不小于B,那么卡A可以覆盖卡片B。作为最好的程序员,你需要计算Alice能覆盖的最大的Bob的牌数。
请注意,每张卡只能使用一次,而卡不能旋转。解题:先用sort排序,两层循环判断,第一层控制a,第二层用来判断a.h大于b.h 用mutiset存入符合条件的a.w 用lower_bound二分b.w 找到了就sum++并且清除mutiset中的a.w#include<iostream> #include<cstdio> #include<set> #include<algorithm> using namespace std; multiset<int>s; struct zz { int h,w; }a[1000007],b[1000007]; bool cmp(zz q,zz p) { if(q.h==p.h) return q.w>p.w; return q.h>p.h; } int main() { int T,n,i,j,sum; scanf("%d",&T); while(T--) { s.clear(); scanf("%d",&n); multiset<int>::iterator it; for(i=0;i<n;i++) scanf("%d%d",&a[i].h,&a[i].w); for(i=0;i<n;i++) scanf("%d%d",&b[i].h,&b[i].w); sort(a,a+n,cmp); sort(b,b+n,cmp); sum=0,j=0; for(i=0;i<n;i++) { while(j<n&&a[j].h>=b[i].h) { s.insert(a[j].w); j++; } it=s.lower_bound(b[i].w); if(it!=s.end()) { sum++; s.erase(it); } } printf("%d\n",sum); } return 0; } 心得:第一次用set做题,看了好多人的代码,get了二分函数yeah~