Test instructions: Alice and Bob have n rectangles, have length and width, one rectangle can cover another rectangle's condition is that the length of itself is greater than or equal to another rectangle, and the width is greater than or equal to another rectangle, the rectangle is not rotatable, ask you Alice can overwrite Bob's several rectangles?
Idea: greedy, first sort by H to Alice and Bob's rectangle, for Alice's every rectangle, if Bob's rectangle's H is less than Alice's H, insert Bob's W into the collection.
Then, if you find the largest Bob's D in the collection that is not greater than Alice Rectangle D, then this is definitely optimal.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = + 5;//const int INF = 0x3f3f3f3f;struct Card {int h, D;}; Card ca[100010], Cb[100010];bool cmp1 (card A, card B) {return A.h < B.h;} multiset<int> Ms;int Main () {//freopen ("Input.txt", "R", stdin); int t; cin >> T;int N; while (t--) {cin >> ; N;ms.clear (); for (int i = 0; i < n; i++) scanf ("%d%d", &ca[i].h, &CA[I].D), for (int i = 0; i < n; i++) scanf (" %d%d ", &cb[i].h, &CB[I].D); sort (ca, Ca+n, cmp1), sort (CB, Cb+n, CMP1), int pos = 0, ans = 0;for (int i = 0; i < n; i++) {while (POS < n) {if (ca[i].h >= cb[pos].h) {Ms.insert (CB[POS].D); pos++;} else break;} if (Ms.empty ()) Continue;multiset<int>::iterator it = Ms.upper_bound (CA[I].D), if (It! = Ms.begin ()) {ans++; Ms.erase (--it);}} cout << ans << endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4268 Alice and Bob (application of greedy +multiset)