您的位置:首页 > 其它

hdu (4268)贪心muliset的upper_bound和lower_bound

2012-09-13 15:12 375 查看
有童鞋A 和 童鞋B

A想用手里的牌尽量多地覆盖掉B手中的牌..

给出了T表示有T组样例..

每组样例给出一个n 表示A和B手中都有n张牌

接下来2*n行 有h w 分别代表A手中n张牌的高和宽 以及 B手中n张牌的高和宽

问A手中的牌最多能覆盖B多少张牌

思路:

对一个坐标排序

假设是x坐标

然后扫描

维护一个y坐标

然后每次取的是堆里面最大的

Tips:

set里的一个函数..在短时间内找到 b的w 中满足小于a的w的值~

iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。

iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。

※ 因为找到的是 >= key的第一个元素..所以为了删除 比当前牌小的..应该it--
Code:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cstdio>
#define N 108000
using namespace std;
struct Node
{
    int h,w;
}alice
,bob
;

bool cmp(Node a,Node b)
{
    if(a.h!=b.h)return a.h<=b.h;
    else return a.w<b.w;
}
int n;

int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
       multiset<int> myset;
        for(int i=0;i<n;i++)
          scanf("%d%d",&alice[i].h,&alice[i].w);
        for(int i=0;i<n;i++)
          scanf("%d%d",&bob[i].h,&bob[i].w);
        sort(alice,alice+n,cmp);
        sort(bob,bob+n,cmp);
        int ans=0;
        int j=0;
        for(int i=0;i<n;i++)
        {
           while(j<n && bob[j].h<=alice[i].h)
            {
                myset.insert(bob[j].w);
                j++;
            }
            if(myset.size()==0)continue;
            set<int>::iterator it=myset.upper_bound(alice[i].w);
            if(it!=myset.begin())
            {
                ans++;
                it--;
                myset.erase(it);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: