HDU 4268 Alice and Bob 37th ACM/ICPC Network Competition 1002 (Greedy + Multiset)

Source: Internet
Author: User
Alice and Bob

Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 627 accepted submission (s): 245

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 input2 2 1 2 3 4 3 4 5 3 3 3 5 7 6 8 4 1 2 5 3 4

 

Sample output1 2

 

Source2012 ACM/ICPC Asia Regional Changchun online

 

Recommendliuyiding: question 2 of the Changchun competition... I didn't think of greedy methods during the competition... Otherwise, you can use Multiset in STL to quickly remove. Worship Twinkle and write the balance binary tree to drop this question! Amazing! In fact, Multiset is implemented internally using a balanced binary tree. The greedy method is to find the maximum value of W to overwrite with H.
 /*  HDU 4268 greedy + STL  */  # Include <Stdio. h> # Include <Math. h> # Include <Iostream> # Include < Set > # Include <Algorithm> Using   Namespace STD;  Const   Int Maxn = 200010  ;  Struct  Node {  Int  H, W;  Int  Flag;} node [maxn];  Bool  CMP (node A, Node B ){  If (A. H! = B .h) Return A. H < B. H; Else   If (A. W! = B. W) Return A. W < B. W;  Else   Return A. Flag> B. Flag;} Multiset < Int > MT; Multiset < Int > : Iterator it;  Int  Main (){  // Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int  T;  Int  N; scanf (  "  % D  " ,& T );  While (T -- ) {Scanf (  "  % D  " ,& N );  For ( Int I = 1 ; I <= N; I ++ ) {Scanf (  "  % D  " , & Node [I]. H ,& Node [I]. W); node [I]. Flag = 1  ;}  For ( Int I = N + 1 ; I <= 2 * N; I ++ ) {Scanf (  "  % D  " , & Node [I]. H ,& Node [I]. W); node [I]. Flag = 2  ;} Sort (node + 1 , Node + 2 * N + 1  , CMP); Mt. Clear ();  Int Ans =0  ;  For ( Int I = 1 ; I <= 2 * N; I ++ ){  If (Node [I]. Flag = 2  ) Mt. insert (node [I]. W );  Else  {  If (! Mt. Empty () {It = Mt. Begin ();  If (Node [I]. W> = (* It) {ans ++ ; It = Mt. upper_bound (node [I]. W); it -- ; Mt. Erase (IT) ;}}} printf (  "  % D \ n  "  , ANS );}  Return  0  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.