HDU 4268 Alice and Bob

Source: Internet
Author: User

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
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

Analysis:

Greedy idea, need to use set.

Today I learned how to set. STL is really powerful. Mark Alice's card as ID = 0, and Bob's card as 1. Set

Some pieces of paper are sorted in ascending order of H and W and in descending order of ID, that is, Bob's paper is put under the same circumstances.

Front. Then insert the W of Bob's paper into the set, greedy to select the W larger than Bob's paper to overwrite.

And then delete the covered paper.

# Include <stdio. h> # include <string. h >#include <algorithm> # include <set> using namespace STD; const int maxn = 200100; typedef Multiset <int> set; typedef Multiset <int >:: iterator P; struct node {int H, W, ID; bool operator <(const node & B) const {If (H! = B. H) return H <B. H; If (W! = B. w) return W <B. w; return ID> B. ID; // sorting process }; node A [maxn]; set D; int main () {int N, T, I, ans; scanf ("% d ", & T); While (t --) {scanf ("% d", & N); for (I = 1; I <= N * 2; I ++) {scanf ("% d", & A [I]. h, & A [I]. w); if (I> N) A [I]. id = 1; else a [I]. id = 0;} Sort (a + 1, A + N * 2 + 1); D. clear (); ans = 0; for (I = 1; I <= N * 2; I ++) {if (a [I]. ID) d. insert (a [I]. w); else if (! D. empty () & * D. begin () <= A [I]. w) {P = D. upper_bound (A [I]. w); // obtain the upper limit ans ++; p --; // The value is smaller than a [I]. the maximum value of w d. erase (p) ;}} 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.