Hdu 4268 Alice and Bob (greedy +multiset+ two points)

Source: Internet
Author: User

Alice and BobTime limit:10000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3511 Accepted Submission (s): 1129


Problem Descriptionalice and Bob ' s game never ends. Today, they introduce a new game. In this game, both of them has N different rectangular cards respectively. Alice wants to use he 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 are not smaller than B. As the best programmer, you is asked to compute the maximal number of Bob's cards that Alice can cover.
Please pay attention this each card can is used only once and the cards cannot be rotated.

Inputthe first line of the input is a number T (T <=) which means the number of test cases.
For each case, the first line was a number N which means the number of cards that Alice and Bob had respectively. Each of the following n (n <= 100,000) lines contains-integers h (H <= 1,000,000,000) and W (w <= 1,000,000,0 XX) 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 a answer using one line which contains just one number.

Sample Input

Sample Output
12

Source2012 ACM/ICPC Asia Regional Changchun Online
test instructions: Alice and Bob each have n cards, each card has a long (h) and a width (w), Bob put his cards in sequence, Alice with his hand to cover, to reach the maximum amount of coverage, the condition is the length and width of the card is greater than or equal to Bob.

The puzzle : greed. A A, a, a, B, the card first by H from small to large row, and then press W from small to large row. To make the maximum number of cards covered, the greedy strategy is to make a card uselessthe smallest card to cover as much as possible in the B card, and closest to it. The current is less than the card Ai.h B card W all thrown into the multiset inside, then two points to find the last less than equals AI.W number, if there is ans++, delete this number.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < string> #include <map> #include <cstdlib> #include <cmath> #include <vector> #include <set    > #include <queue>using namespace std;const int maxn=1e5+5;struct T {int h;    int W;    BOOL operator < (const T & A) const {return h<a.h; }};void Read (T * a,int N) {for (int i=0; i<n; i++) scanf ("%d%d", &AMP;A[I].H,&AMP;A[I].W);} T A[MAXN];    T b[maxn];int ans;int n;multiset<int> st;multiset<int>::iterator p;int main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Read (a,n);        Read (b,n);        Sort (a,a+n);        Sort (b,b+n);        St.clear ();        ans=0;        int j=0;            for (int i=0, i<n; i++) {while (j<n&&b[j].h<=a[i].h) St.insert (B[J++].W);            if (St.empty ()) continue; P=st.lower_bound (A[I].W);            if (P==st.end () | | |            *p>a[i].w&&p!=st.begin ()) p--;                if (*P&LT;=A[I].W) {ans++;            St.erase (P);    }} printf ("%d\n", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 4268 Alice and Bob (greedy +multiset+ two points)

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.