您的位置:首页 > 其它

HDU 4268 Alice and Bob (贪心)

2014-09-25 21:23 519 查看
对于 A中的 每一个扑克牌 在 x 可以覆盖的 范围内。 应该保证 被覆盖的 y尽量大。 这样 才是不浪费 y的。 并且 能够保证 后面的 A的 x更大的时候

出现 y 很小的时候 可以 覆盖 那些 x 小 并且 y 相对较小的。 这个思想很好的。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cmath>
#include <iomanip>
typedef long long LL;
typedef unsigned long long LLU;
const double PI=acos(-1.0);
using namespace std;
#define MAXN 100000+10
struct card{
    int x,y;
    friend bool operator < (card a, card b){
        if(a.x != b.x)
            return a.x < b.x;
        return a.y < b.y;
    }
};
card a[MAXN],b[MAXN];
int main (){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
            scanf("%d%d",&a[i].x, &a[i].y);
        for(int i = 0; i < n; i++)
            scanf("%d%d",&b[i].x, &b[i].y);
        sort(a,a+n);
        sort(b,b+n);
        multiset <int> s;
        int p = 0;
        int ans = 0;
        for(int i = 0; i < n; i++){
            while(p < n && b[p].x <= a[i].x){
                s.insert(b[p].y);
                p++;
            }
            if(s.size() >= 1){
                multiset <int> ::iterator it = s.lower_bound(a[i].y);
                if(it == s.end())
                    it--;
                if(it != s.begin() && (*it) > a[i].y){
                    it--;
                }
                if((*it) <= a[i].y){
                    s.erase(it);
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: