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




目录
相关文章
|
机器学习/深度学习
集合论—笛卡尔积与二元关系
集合论—笛卡尔积与二元关系
|
算法
KMP算法
KMP算法
266 0
|
NoSQL 关系型数据库 MySQL
聊聊高并发下超卖,少卖的解决方案
聊聊高并发下超卖,少卖的解决方案
818 0
|
机器学习/深度学习
【配电网规划】SOCPR和基于线性离散最优潮流(OPF)模型的配电网规划( DNP )(Matlab代码实现)
【配电网规划】SOCPR和基于线性离散最优潮流(OPF)模型的配电网规划( DNP )(Matlab代码实现)
287 0
|
消息中间件 存储 分布式计算
商汤科技面试总结(上)
一、kafka数据分区和消费者的关系,kafka的数据offset读取流程,kafka内部如何保证顺序,结合外部组件如何保证消费者的顺序
425 0
商汤科技面试总结(上)
|
19天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23541 13
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
7天前
|
Shell API 开发工具
Claude Code 快速上手指南(新手友好版)
AI编程工具卷疯啦!Claude Code凭借任务驱动+终端原生的特性,成了开发者的效率搭子。本文从安装、登录、切换国产模型到常用命令,手把手带新手快速上手,全程避坑,30分钟独立用起来。
2157 13
|
4天前
|
人工智能 开发工具 iOS开发
Claude Code 新手完全上手指南:安装、国产模型配置与常用命令全解
Claude Code 是一款运行在终端环境中的 AI 编程助手,能够直接在命令行中完成代码生成、项目分析、文件修改、命令执行、Git 管理等开发全流程工作。它最大的特点是**任务驱动、终端原生、轻量高效、多模型兼容**,无需图形界面、不依赖 IDE 插件,能够深度融入开发者日常工作流。
1565 2

热门文章

最新文章