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", &A[I].H,&A[I].W); for (int i=0;i<n;i++) scanf ("%d%d", &B[I].H,&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<=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)