HDU 4268 Alice and Bob (set + greedy)

Source: Internet
Author: User
Alice and Bob

Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 1796 accepted submission (s): 641 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

 
221 23 42 34 532 35 76 84 12 53 4

Sample output

 
12

source2012 ACM/ICPC Asia Regional Changchun online feeling: when I saw this question during the competition, I felt very greedy, however, later I thought it was not that simple. The key was to set and balance the tree by myself. If it was too complicated, I could make it greedy.
train of thought: sort the rectangles of two people (in the order of H, W, and Id) and scan them again to find the nearest rectangle matching the conditions. (If multiple rectangles can be covered, the maximum value is W.)
PS: sorry, I started using the set. I thought it would be enough to use the set, but the elements in the set cannot be repeated. So in some cases, there will be an error when using the hidden elements) hangzhou-Hangzhou... The idea is the same. Changing set to Multiset is enough to thank Quan for reminding me
Code :

# Include <iostream> # include <cstdio> # include <set> # include <algorithm> # define maxn 200005 using namespace STD; int n, m, ans; struct node {int H, W, ID;} node [maxn]; Multiset <int> S; Multiset <int>: iterator ite; bool CMP (const node & X1, const node & x2) {If (x1.h! = X2.h) return x1.h <x2.h; If (x1.w! = X2.w) return x1.w <x2.w; return x1.id <x2.id;} int main () {int I, j, T; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); for (I = 1; I <= 2 * n; I ++) {scanf ("% d", & node [I]. h, & node [I]. w); node [I]. id = (I <= N);} Sort (node + 1, node + 2 * n + 1, CMP); // sort s in the order of H, W, and ID. clear (); ans = 0; for (I = 1; I <= 2 * n; I ++) {If (! Node [I]. ID) s. insert (node [I]. w); else {If (S. empty () | node [I]. W <* (S. begin () continue; // skip ite = s when the value of S is null. upper_bound (node [I]. w); // if the value of W is greater than or equal to him, the value of the previous W is definitely less than the value of ITE --; ans ++; S. erase (ITE); // Delete after overwriting} printf ("% d \ n", ANS);} return 0;}/* 132 310 81 24 85 8 */

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.