hdu 4268 Alice and Bob

简介: 点击打开链接4268 1题目:                                                                    Alice and Bob Problem Description Alice and Bob's game never ends.

点击打开链接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;
}




目录
相关文章
|
7月前
|
Python
Python实现Tom与Jerry
Python实现Tom与Jerry
61 0
|
4月前
|
存储 C++ 索引
【面试题】找到 Alice 和 Bob 可以相遇的建筑
【面试题】找到 Alice 和 Bob 可以相遇的建筑
16 0
|
6月前
|
定位技术
牛和 John
牛和 John
38 0
|
人工智能 算法 BI
C++二分查找算法:找到 Alice 和 Bob 可以相遇的建筑
C++二分查找算法:找到 Alice 和 Bob 可以相遇的建筑
|
数据安全/隐私保护 Python
BUUCTF Alice与Bob 1
BUUCTF Alice与Bob 1
148 0
[HDU7073] Integers Have Friends 2.0 -随机大法好
题意: 找到最大的一个集合,使得集合内所有元素 % m(>=2) 问最大的集合大小
96 0
[HDU7073] Integers Have Friends 2.0 -随机大法好
[POJ 3683] Priest John‘s Busiest Day | 2-SAT +Tarjan缩点跑拓扑排序
题意: 给出n个婚礼,每个婚礼有个开始的时间和结束的时间,在婚礼期间,要举行持续时间为D的活动,这个活动只能在婚礼期间的前D时间内举行,或者是在婚礼期间的后D时间内举行,问能否安排一种方式使得能够参与n个婚礼的活动 思路: 每个活动有两个选择,而且还要满足一定的条件,这显然是一个2-SAT问题 方法: 2-SAT + Tarjan 首先根据两种选择是否首尾冲突进行连边,如果冲突,则连对立的边
100 0
|
JavaScript UED 前端开发
|
机器学习/深度学习 Java 网络架构