HDU 4268 Alice and Bob (application of greedy +multiset)

Source: Internet
Author: User



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 >&gt ; N;ms.clear (); for (int i = 0; i < n; i++) scanf ("%d%d", &ca[i].h, &AMP;CA[I].D), for (int i = 0; i < n; i++) scanf ("  %d%d ", &cb[i].h, &AMP;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)

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.