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;
}




目录
相关文章
HDOJ-1004 Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
1023 0
|
22小时前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
10天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
4天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
429 191
|
3天前
|
数据采集 消息中间件 人工智能
跨系统数据搬运的全方位解析,包括定义、痛点、技术、方法及智能体解决方案
跨系统数据搬运打通企业数据孤岛,实现CRM、ERP等系统高效互通。伴随数字化转型,全球市场规模超150亿美元,中国年增速达30%。本文详解其定义、痛点、技术原理、主流方法及智能体新范式,结合实在Agent等案例,揭示从数据割裂到智能流通的实践路径,助力企业降本增效,释放数据价值。
|
8天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。