hdu 4268 Alice and Bob

点击打开链接4268


1题目:

Alice and Bob

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

2思路: 贪心

3分析: Alice的要去覆盖Bob的卡片,如果要求能够尽量盖的多,就是Alice的卡片要尽量盖住Bob大的卡片,这样浪费最少才能够达到最优的效果。

4解题过程: 1 对Alice 和 Bob 排序 先按h排再按w排。

2枚举Alice的每一张卡片,同时将Bob中长度小于Alice的卡片的宽度全部插入mulitiset的容器s里面(这里默认使用小于号排序这样可以减少排序的操作),然后进行利用upper_bound(Alice[i].w)查找到Bob卡片的宽大于Alice的宽的第一个位置it。判断如果it是在第一个,那么不用it--,不然会越界。然后判断当前的*it是否小于等于
Alice[i].w并且容器不为空,满足条件的话ans++,同时删除it;


5代码


#include <algorithm>  
#include <iostream> 
#include <cstring>
#include <cstdio>
#include <set>
using namespace std;
#define MAXN 100010   
#define INF 0xFFFFFFF

int t , n , ans;
struct card{
    int h;
    int w;
}Alice[MAXN] , Bob[MAXN];
multiset<int>s;

bool cmp(card c1 , card c2){
    if(c1.h < c2.h) return true;
    else if(c1.h == c2.h && c1.w < c2.w) return true;
    return false;
}

void solve(){
    int i , j;
    sort(Alice , Alice+n , cmp);/*对Alice排序*/
    sort(Bob , Bob+n , cmp);/*对Bob*/
    ans = 0 ; 
    s.clear() ; j = 0;
    multiset<int>::iterator it;
    for(i = 0 ; i < n ; i++){
        while(j < n){
            if(Alice[i].h >= Bob[j].h){/*将高度小于Alice的找出来,把宽度插入容器*/
                s.insert(Bob[j].w);
                j++;
            }
            else break;/*否则退出*/
        }
        it = s.upper_bound(Alice[i].w);/*利用upper_bound()找到位置*/
        if(s.size() > 0 && it != s.begin()) it--;
        if(s.size() > 0 && (*it <= Alice[i].w)){
            ans++;
            s.erase(it);
        }
    }
    printf("%d\n" , ans);
}

int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%d" , &t);
    while(t--){
        scanf("%d" , &n);
        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);
        solve();
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值