HDU4268 Alice and Bob(贪心+multiset)
Please pay attention that each card can be used only once and the cards cannot be rotated.
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.
一开始用了贪心以后想用alice的最小w从bob最小w开始选,答案当然错了,应该用alice的最小w去选bob尽可能大的w。用了两个for,超时,于是用multiset,让时间复杂度瞬间降低,应该变成O(n)了。
#include<stdio.h>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set> using namespace std; struct card
{
int h,w;
}; int cmp(card a,card b)
{
if(a.h==b.h)
{
return a.w<b.w;
}
return a.h<b.h;
} int main()
{
int k,m,q,t,p,n;
int T;
cin>>T;
while(T--)
{
int ans=;
card a[],b[];
multiset<int> ms;
multiset<int>::iterator it; cin>>n;
for(int i=;i<n;++i)
{
scanf("%d %d",&a[i].h,&a[i].w);
}
for(int i=;i<n;++i)
{
scanf("%d %d",&b[i].h,&b[i].w);
} sort(a,a+n,cmp);
sort(b,b+n,cmp); int i=,j=;
for(;i<n;++i)
{
while(j<n&&(a[i].h>=b[j].h))
{
ms.insert(b[j].w);
++j;
}
if(ms.empty())
{
continue;
} it=ms.upper_bound(a[i].w);
if(it==ms.begin())
{
continue;
}else
{
--it;
++ans;
ms.erase(it);
} }
cout<<ans<<endl; }
return ;
}
HDU4268 Alice and Bob(贪心+multiset)的更多相关文章
- HDU4268 Alice and Bob 【贪心】
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Alice and Bob(贪心HDU 4268)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4268 Alice and Bob 贪心STL O(nlogn)
B - Alice and Bob Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- HDU 4268 Alice and Bob(贪心+Multiset的应用)
题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...
- hdu 4268 Alice and Bob(multiset|段树)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4268 Alice and Bob
Alice and Bob Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- Alice and Bob(mutiset容器)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Alice and Bob HDU - 4268
Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
随机推荐
- ARM&Linux 下驱动开发第一节(小试牛刀)
#include<linux/init.h> #include<linux/module.h> static int __init hello_init(void) { pri ...
- matlab reshape函数
语法 (1)B = reshape(A,m,n) 使用方法: B=reshape(A,m,n) 返回m*n矩阵B,它的元素是获得A的行宽度.假设A没有m*n元素,得到一个错误结果. 样例: <s ...
- linux C高手成长过程---书籍推荐
建议学习路径: 首先先学学编辑器,vim, emacs什么的都行. 然后学make file文件,只要知道一点就行,这样就可以准备编程序了. 然后看看<C程序设计语言>K&R,这样 ...
- [AngularJS] Best Practise - Minification and annotation
Annotation Order: It's considered good practice to dependency inject Angular's providers in before o ...
- 关于Android 访问权限设置
我前几天在做同城交友网(www.niyuewo.com)与医药网(www.yiyaojing.com)时遇到的问题整理如下: Android开发应用程序时,如果应用程序需要访问网络权限,需要在 And ...
- 实现O(1)获取最大最小值的栈----java
原文:http://blog.csdn.net/sheepmu/article/details/38459165 实现O(1)获取最大最小值的栈和队列----java 一.如何实现包含获取最小值函数的 ...
- C# 模式窗口下更新进度条
public partial class frmProcessBar : Form { public frmProcessBar() { InitializeComponent(); } public ...
- C语言第六节基本运算符
算术运算 C语言一共有34种运算符,包括了常见的加减乘除运算 加法运算+ 除开能做加法运算,还能表示正号:+5.+90 减法运算- 除开能做减法运算,还能表示符号:-10.-29 乘法运算* 注意符号 ...
- iOS 虚拟机测试出现的相关问题
一.报红 1.问题描述:自己在工程目录下新建文件夹 包其他文件夹下的.h 和.m文件拖到该文件夹 再删除工程中的报红文件复制进去新文件夹 运行报错:No such file or directory: ...
- 重构18-Replace exception with conditional(条件替代异常)
重构没有什么出处,是我平时经常使用而总结出来的.欢迎您发表任何改进意见或建议.我相信一定还有其他比较好的重构可以解决类似的问题. 我曾无数次面对的一个代码坏味道就是,使用异常来控制程序流程.您可能会 ...