贪心+multiset · HDU - 4268 ·Alice and Bob

题目大意:

Alice有一堆卡片,Bob有一堆卡片,问A中有几张卡片能覆盖B中的。

解题思路:

首先题意就埋了点坑(可能是自己SB):

Please pay attention that each card can be used only once and the cards cannot be rotated. 

这句我开始理解成了A中的只能用一次,所以我就直接找到B中h最小,w相对最小的来对A中卡片做O(n)查询。理所应当的WA了。应该是A和B中的卡片都只能用一次;

大体思路就是使用基本的贪心策略按照h排序再对w排序,找到B中能被A覆盖的卡片。

这里用到了multiset这玩意中的一个函数upper_bound,首先我自己没想到用这个,因为我之前也不知道,主要是我这种思路是,找到B中比A中当前卡片h小,但w最大的一张卡片,有点不太好处理,就冥(百)想(度)到了方法。

批判网上一些博客,代码都是错的,lower_bound是找到≥的第一个位置,有的人找到后直接it--,你就不怕越界吗,害劳资想了半天,然后交了他的代码发现真是错的,当时是upper_bound找到大于的再--了,,还有这题数据比较弱,在操作绝对正确的情况下,不小心用了lower_bound,趁judge机不注意交一发也能过,,,,,,

AC代码:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
//#define   maxd          1010
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define   mc(x, y)     memcpy(x, y, sizeof(x))
#define   ms(x,y)      memset(x,y,sizeof(x))
#define   rep(i,n)      for(int i=0;i<(n);i++)
#define   repf(i,a,b)   for(int i=(a);i<=(b);i++)
#define   PI           pair<int,int>
//#define   mapp            make_pair
#define   FI            first
#define   SE            second
#define   IT            iterator
#define   PB            push_back
#define   Times         10
typedef   long long     ll;
typedef   unsigned long long ull;
typedef   long double   ld;
typedef   pair<int,int > pce;
//#define N 100
const double eps = 1e-10;
const double  pi = acos(-1.0);
const  ll    mod = 1e9+7;
const  int   inf = 0x3f3f3f3f;
//const  ll    INF = (ll)1e18+300;
const int   maxd = 101000 + 10;
const int   maxx = 10100;

int n;
struct node {
    int h, w;
    //node();
    node(int h = 0, int w = 0) {
        this->h = h;
        this->w = w;
    }
};
bool cmp(node a, node b) {
    if(a.h != b.h)return a.h < b.h;
    else return a.w < b.w;
}
node ac[maxd];
node bc[maxd];
int main() {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t --) {
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> ac[i].h >> ac[i].w;
        }
        for (int i = 0; i < n; i++) {
            cin >> bc[i].h >> bc[i].w;
        }
        sort(ac, ac+n, cmp);
        sort(bc, bc+n, cmp);
        int ans = 0;
        multiset<int > mts;
        multiset<int >::iterator it;
        for (int i = 0, j = 0; i < n; i++) {
            for ( ; j < n; ) {
                if(ac[i].h >= bc[j].h) {
                    mts.insert(bc[j].w);
                    j ++;
                }
                else break;
            }
            if(!mts.empty() && *mts.begin() <= ac[i].w) {
                it = mts.upper_bound(ac[i].w);
                //cout << *it << endl;
               // cout <<ac[i].w << " " << *it << "+++++++++" << endl;
                //if(*it >= ac[i].w) {
                    it --;
               // }
                ans ++;
                mts.erase(it);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章