Hdu-4268-Alice and Bob

Source: Internet
Author: User

Alice and Bob both have N Rectangles and ask Alice how many rectangles can cover Bob at most. Each rectangle can be used only once, and the width and height cannot be rotated. --> Greedy, the first thought of strategy is: sort the rectangle in descending order of width and height, and then Alice uses the widest to cover Bob's widest, the result is WA. Now we find that the direction is reversed! The greedy strategy should be from small to large, so that every rectangle of Alice can exert its maximum value, that is, take one of them: width, access from small to large, each access one, find all the satisfied heights from small to large in height, and cover the largest one as far as possible (multiset has the search function upper_bound (x), and returns the first iterator greater than x ). It's a good taste to use pair and multiset for the first time ~~ [Cpp] # include <cstdio> # include <vector> # include <set> # include <algorithm> using namespace std; typedef pair <int, int> pii; const int maxn = 100000 + 10; int main () {int T, N, I, j, h, w; scanf ("% d", & T ); while (T --) {scanf ("% d", & N); vector <pii> A, B; for (I = 0; I <N; I ++) {scanf ("% d", & h, & w);. push_back (make_pair (h, w) ;}for (I = 0; I <N; I ++) {scanf ("% d", & h, & w); B. push_back (make _ Pair (h, w);} sort (. begin (),. end (); sort (B. begin (), B. end (); int cnt = 0; multiset <int> s; s. clear (); for (I = 0, j = 0; I <N; I ++) {while (j <N & A [I]. first> = B [j]. first) s. insert (B [j ++]. second); if (! S. empty () www.2cto.com {multiset <int>: iterator p = s. upper_bound (A [I]. second); if (p! = S. begin () {s. erase (-- p); cnt ++ ;}} printf ("% d \ n", cnt) ;}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.