hdu4268 Alice and Bob(贪心+set)

hdu4268
题意:A和B两个人各有n个矩形板子,当A的板子的长>=B的板子的长&&A的板子的宽>=B的板子的宽时,则是覆盖,求最大能覆盖的数量。
题解:两个序列的长都从小到大排序,然后从B中找出满足小于等于A的第i板子的长,先满足一个条件,塞到multiset里,再找出满足第一个条件后set中最大的宽,ans++。遍历完所有的A板子即可。
set:排序+去重
muliset:排序
set用法参考我以前的一篇博客https://blog.csdn.net/zxy160/article/details/76461724

#include<stdio.h>
#include<string>
#include<queue>
#include<math.h>
#include<map>
#include<set>
#include<stack>
#include<string.h>
#include<iostream>
#include<algorithm>
#define PI acos(-1.0)
typedef long long ll;
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=1e5+100;
multiset<int>s;
struct node
{
    int l,r;
    bool operator<(const node &b) const
    {
        return l<b.l;
    }
} e1[maxn],e2[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        s.clear();
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d%d",&e1[i].l,&e1[i].r);
        for(int i=0; i<n; i++)
            scanf("%d%d",&e2[i].l,&e2[i].r);
        sort(e1,e1+n);
        sort(e2,e2+n);
        int ans=0;
        for(int i=0,j=0; i<n; i++)
        {
            while(j<n&&e1[i].l>=e2[j].l)
            {
                s.insert(e2[j].r);
                j++;
            }
            set<int>::iterator it=s.upper_bound(e1[i].r);
            if(s.size()>0&&it!=s.begin())
                it--;
            if(s.size()>0&&*it<=e1[i].r)
            {
                ans++;
                s.erase(it);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值