hdu 4268 Alice and Bob (set+贪心)

Alice and Bob

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1796    Accepted Submission(s): 641


Problem Description
Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N different rectangular cards respectively. Alice wants to use his cards to cover Bob's. The card A can cover the card B if the height of A is not smaller than B and the width of A is not smaller than B. As the best programmer, you are asked to compute the maximal number of Bob's cards that Alice can cover.
Please pay attention that each card can be used only once and the cards cannot be rotated.
 

Input
The first line of the input is a number T (T <= 40) which means the number of test cases.
For each case, the first line is a number N which means the number of cards that Alice and Bob have respectively. Each of the following N (N <= 100,000) lines contains two integers h (h <= 1,000,000,000) and w (w <= 1,000,000,000) which means the height and width of Alice's card, then the following N lines means that of Bob's.
 

Output
For each test case, output an answer using one line which contains just one number.
 

Sample Input
  
  
2 2 1 2 3 4 2 3 4 5 3 2 3 5 7 6 8 4 1 2 5 3 4
 

Sample Output
  
  
1 2
 

Source
 
感想:比赛时一看到这题,感觉是个很水的贪心,不过后来又觉得不是那么简单,关键是自己set、平衡树不会啊,太菜了,会一点都能把这个贪心做出来了。

思路:将两个人的矩形一起排序(按h、w、id顺序),然后扫描一遍,找满足条件的最近矩形对。(如果一个矩形有多个能覆盖的,则覆盖w最大的)

ps:亲们 不好意思 开始用set过了 以为用set就够了  不过set里面的元素不允许重复 所以有些情况会出错 ╮(╯▽╰)╭  杭电的数据又一次水了。。。 思路都是一样的 把set改为multiset就够了  感谢泉泉的提醒

代码:
#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
#define maxn 200005
using namespace std;

int n,m,ans;
struct Node
{
    int h,w,id;
}node[maxn];
multiset<int>s;
multiset<int>::iterator ite;

bool cmp(const Node&x1,const Node&x2)
{
    if(x1.h!=x2.h) return x1.h<x2.h;
    if(x1.w!=x2.w) return x1.w<x2.w;
    return x1.id<x2.id;
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=2*n;i++)
        {
            scanf("%d%d",&node[i].h,&node[i].w);
            node[i].id=(i<=n);
        }
        sort(node+1,node+2*n+1,cmp);  // 按 h、w、id 的顺序排序
        s.clear();
        ans=0;
        for(i=1;i<=2*n;i++)
        {
            if(!node[i].id) s.insert(node[i].w);
            else
            {
                if(s.empty()||node[i].w<*(s.begin())) continue ;  // s为空的时候也直接跳过
                ite=s.upper_bound(node[i].w); // 找到w大于等于他的 则他的上一个w肯定小于他
                ite--;
                ans++;
                s.erase(ite);  // 覆盖后删除
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
1
3
2 3
10 8
6 8
1 2
4 8
5 8
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值