HDU 4268 Alice and Bob (greedy +stl)

Source: Internet
Author: User

At the beginning of the question I wrote a pitch with Multiset, the more naked, tle. Later, the teammates thought of a better way: After the two structure is sorted, from the large to the small enumeration Alice's H, for each h, Bob to meet H is less than the current H of the card W plus multiset, and then use the dichotomy function to find a greater than or equal to the current W of the first number, The previous pointer of the current iterator is the largest w that is less than the current W, and is deleted when found.  So we used this greedy strategy: for Alice's every card, to cover a Bob's as far as H and W close to its cards. Why is such a greedy strategy correct? If not, then the current card will cover a w smaller card, if after the card has a W than the current card is small, then this is not cost-effective, if there is a card w than the current card, then obviously can not be better.

See the code for details:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < vector> #include <map> #include <list> #include <cmath> #include <set> #include <queue>    Using namespace Std;typedef long long ll;const long long maxn = 100000+5;int t,n,m;struct node{int h,w; BOOL operator < (const node& RHS) Const {return h < Rhs.h | |    (h = = Rhs.h && W < RHS.W);    }}a[maxn],b[maxn];multiset<int> G;int Main () {scanf ("%d", &t);        while (t--) {g.clear ();        scanf ("%d", &n);        for (int i=0;i<n;i++) scanf ("%d%d", &AMP;A[I].H,&AMP;A[I].W);        for (int i=0;i<n;i++) scanf ("%d%d", &AMP;B[I].H,&AMP;B[I].W);        Sort (a,a+n);        Sort (b,b+n);        int cur = 0,cnt=0;            for (int. i=0;i<n;i++) {while (cur<n&&b[cur].h<=a[i].h) G.insert (B[CUR++].W);            if (G.size () ==0) continue; Multiset<int>:: Iterator it = G.lower_bound (a[i].W);            if (It!=g.begin ()) it--;        if (*IT&LT;=A[I].W) g.erase (it), cnt++;    } printf ("%d\n", CNT); } return 0;}


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

HDU 4268 Alice and Bob (greedy +stl)

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.