貪心+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;
}

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