2012 ACM/ICPC Asia Regional Changchun Online(Alice and bob)

本文介绍了一个经典的矩形覆盖问题,通过贪心算法实现,利用set容器和upper_bound函数找到能够覆盖的最大数量。输入包括测试案例数量及每个案例中矩形的数量、尺寸等。
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
题意:给定待包围的矩形若干,用若干个矩形去包围它。最多可以包围多少个。
思路:贪心的思想。从小到大覆盖是满足长覆盖的情况下应该使得覆盖的宽尽量大。将矩形的长宽排序。采用标记的方法可以区分覆盖的和被覆盖的矩形。
set容器和upper_bounder函数能很好的解决
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
struct lmx{
    int x;
    int y;
    int num;
};
bool is(lmx s,lmx t)
{
     //if(s.num!=t.num) return s.num>t.num;
    if(s.x!=t.x) return s.x<t.x;
    if(s.y!=t.y)  s.y<t.y;
    return s.num<t.num;
}
lmx lm[200005];
int main()
{
    int test,i,n,cnt;
    set<int> s;
    scanf("%d",&test);
    while(test--)
    {
        s.clear();
        cin>>n;
        cnt=0;
        for(i=0;i<n;i++)
        {
           scanf("%d%d",&lm[i].x,&lm[i].y);
           lm[i].num=1;
        }
        for(i=n;i<2*n;i++)
        {
            scanf("%d%d",&lm[i].x,&lm[i].y);
            lm[i].num=0;
        }
        sort(lm,lm+2*n,is);
        for(i=0;i<2*n;i++)
        {
            if(lm[i].num==0) s.insert(lm[i].y);
            else
            {
                if(!s.empty()&&*s.begin()<=lm[i].y)
                {
                    set<int>::iterator it=s.upper_bound(lm[i].y);
                    it--;
                    cnt++;
                    s.erase(it);
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/ffhuguang/p/3141471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值