Day3-H-Alice and Bob HDU4268
Please pay attention that each card can be used only once and the cards cannot be rotated.
InputThe 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.
OutputFor 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 思路:依旧是贪心,但这个是二维问题,我们可以类比二位偏序问题来处理,先对h排序,扫描一遍Alice的数组,将每个小于等于h的Bob的元素入队,然后将小于等于w的元素出队
记录个数,此时无需关心h,因为入队的元素一定不大于现在的h,这里我们就要用到multiset这个容器来"当作队列",因为其支持upper_bound/lower_bound的二分查找操作,
具体细节用代码感受一下:
struct Node {
int h, w;
Node(int _h = , int _w = ) : h(_h), w(_w){}
bool operator<(const Node &a)const {
return h < a.h || (h == a.h && w < a.w);
}
};
vector<Node> v[];
multiset<int> q;
int main() {
int T, n, sum;
scanf("%d", &T);
while(T--) {
q.clear();
sum = ;
scanf("%d", &n);
for (int i = ; i < ; ++i) {
v[i].clear();
for (int j = ; j < n; ++j) {
int t1, t2;
scanf("%d%d", &t1, &t2);
v[i].push_back(Node(t1, t2));
}
}
sort(v[].begin(), v[].end()), sort(v[].begin(), v[].end());
for (int i = , j = ; i < n; ++i) {
while(j < n && v[][i].h >= v[][j].h) {
q.insert(v[][j++].w);
}
if(!q.empty() && *q.begin() <= v[][i].w) {
++sum;
auto tmp = q.upper_bound(v[][i].w);
q.erase(--tmp);
}
}
printf("%d\n", sum);
}
return ;
}
提一下为什么用upper_bound并递减,因为当此处的w比队列中任意元素大时,两种查找都返回end()迭代器,此时需要递减操作,如果w是中间大小元素,lower_bound的递减就会出错,当然特判一下也可以,等价的。
Day3-H-Alice and Bob HDU4268的更多相关文章
- HDU4268 Alice and Bob(贪心+multiset)
Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...
- HDU4268 Alice and Bob 【贪心】
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- bzoj4730: Alice和Bob又在玩游戏
Description Alice和Bob在玩游戏.有n个节点,m条边(0<=m<=n-1),构成若干棵有根树,每棵树的根节点是该连通块内编号最 小的点.Alice和Bob轮流操作,每回合 ...
- Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...
- sdutoj 2608 Alice and Bob
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...
- hdu 4268 Alice and Bob
Alice and Bob Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- SDUT 2608:Alice and Bob
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...
- Alice and Bob(贪心HDU 4268)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- UVA 1484 - Alice and Bob's Trip(树形DP)
题目链接:1484 - Alice and Bob's Trip 题意:BOB和ALICE这对狗男女在一颗树上走,BOB先走,BOB要尽量使得总路径权和大,ALICE要小,可是有个条件,就是路径权值总 ...
- ZOJ 3757 Alice and Bob and Cue Sports(模拟)
题目链接 题意 : 玩台球.Alice 和 Bob,一共可以进行m次,Alice 先打.有一个白球和n个标有不同标号的球,称目标球为当前在桌子上的除了白球以外的数值最小的球,默认白球的标号为0.如果白 ...
随机推荐
- 杨蓉庆201771010135《面向对象程序设计(java)》第一周学习总结
第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com 艾特大家 程序设计评测:https://pintia.cn/ 艾特你 代码托管平台:h ...
- GO TIME
#go语言的time包 ##组成 time.Duration(时长,耗时) time.Time(时间点) time.C(放时间点的管道)[ Time.C:=make(chan time.Time) ] ...
- 【原】tcp三次握手和四次挥手
- 自定义配置 const
自定义常量配置文件:settings/const.py # 自定义的常量配置文件,在settings中 from 该文件 import *,将名字全部丢给settings BANNER_COUNT = ...
- IDEA & MAVEN配置代理(没用)
1. IDEA配置代理: 2. maven配置代理: 在maven中配置代理,主要配置编辑~/.m2/settings.xml文件的<proxies> socks5类型: <id&g ...
- 「Luogu P5080 Tweetuzki 爱序列」
题目大意 给出一些数,需要求出 \(\frac{a_{i+1}}{3}=a_i\) 或 \(a_{i+1}=2 \times a_i\) 时最长的序列 \(a\). 分析 可以发现符合条件的序列 \( ...
- echarts做飞线图
先上图,要不感觉没有说服力: 飞线图应该是大屏中很常见的一种了,通常你可以很轻易的用datav做一个飞线图,而且datav做的大屏逼格真的很高,本身也是开源免费的项目,开箱即用,上手简单……行了回归正 ...
- Day11 - Q - A Multiplication Game HDU - 1517
题目链接 本题很像bash博弈,但又有些许不同,因为这里是乘法,我们可以列出前几项可能 若n=2-9,那么first可以一次取完 若n=10-18,无论first怎么取,second都能一次取完 若n ...
- springBoot+MybatisPlus数据库字段使用驼峰命名法时报错
假如有个实体类: package com.jeff.entity; public class User { /** * 主键id */ private Integer id; /** * 登陆名 */ ...
- Windows 安装python虚拟环境
windows 安装pytho虚拟环境 方法一:virtualenv (1)使用pip安装virtualenv工具 pip install virtualenv (2)使用virtualenv创建虚拟 ...