hdu4268 Alice and Bob
来源:互联网 发布:江苏移动网络怎么样 编辑:程序博客网 时间:2024/05/17 07:05
Alice and Bob
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5003 Accepted Submission(s): 1574
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.
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.
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
221 23 42 34 532 35 76 84 12 53 4
Sample Output
12
Source
2012 ACM/ICPC Asia Regional Changchun Online
Recommend
liuyiding | We have carefully selected several similar problems for you: 4276 4274 4272 4277 4267
普通的双重循环会超时,另外使用c++的输出方式也会超时,这一点真的好坑啊。
利用multiset来解答问题。 这个容器和set的不同点就是允许有重复值。先对两个数组排序,之后遍历第一个数组,设当前为第i个,我们需要满足两个条件,我们分开判断,先判断满足第一个条件,也就是长度的。while循环把第二个数组所有长小于等于i的长度的数组的宽全部放进容器当中(注意不要重复放入,也就是说while循环进入的就是进入了,下一次遍历第一个数组时要接着来,不要从0开始遍历第二个数组) 。之后利用lower_bound查找第一个大于等于第i个的宽的值。如果没有查到,continue;如果正好相等,数量+1,利用erase把那个宽删除,还有一个情况,直接看代码
题目总结:对于两个判断条件,可以选择利用容器先判断一个,在判断另一个。节约时间。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<set>
using namespace std;
int t;
int n;
int k;
struct node
{
int x,y;
};
bool cmp(node x1,node x2)
{
if(x1.x==x2.x)
return x1.y<x2.y;
return x1.x<x2.x;
}
node t1[1000010];
node t2[1000010];
multiset <int> s;
multiset<int>::iterator it;
int cnt;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
s.clear();
for(int i=1;i<=n;i++)
{
scanf("%d%d",&t1[i].x,&t1[i].y);
}
for(int i=1;i<=n;i++)
{
scanf("%d%d",&t2[i].x,&t2[i].y);
}
sort(t1+1,t1+n+1,cmp);
sort(t2+1,t2+n+1,cmp);
cnt=0;
k=1;
for(int i=1;i<=n;i++)
{
while(k<=n&&t2[k].x<=t1[i].x)
{
s.insert(t2[k].y);
k++;
}
if(s.empty())
{
continue;
}
it=s.lower_bound(t1[i].y);
if(*it==t1[i].y)
{
cnt++;
s.erase(it);
}
else
{
if(it==s.begin())
{
continue;
}
else
{
s.erase(--it); //这里为什么要提前减一。 我们查询的是大于等于i的宽的第一个值。如果等于,最好,前面也判断了。 如果不等于,我们肯定要往前找,只要往前1个,那肯定就是小于i的宽了。 这是最优值,也就是保证可以覆盖情况下,最大的选择
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<set>
using namespace std;
int t;
int n;
int k;
struct node
{
int x,y;
};
bool cmp(node x1,node x2)
{
if(x1.x==x2.x)
return x1.y<x2.y;
return x1.x<x2.x;
}
node t1[1000010];
node t2[1000010];
multiset <int> s;
multiset<int>::iterator it;
int cnt;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
s.clear();
for(int i=1;i<=n;i++)
{
scanf("%d%d",&t1[i].x,&t1[i].y);
}
for(int i=1;i<=n;i++)
{
scanf("%d%d",&t2[i].x,&t2[i].y);
}
sort(t1+1,t1+n+1,cmp);
sort(t2+1,t2+n+1,cmp);
cnt=0;
k=1;
for(int i=1;i<=n;i++)
{
while(k<=n&&t2[k].x<=t1[i].x)
{
s.insert(t2[k].y);
k++;
}
if(s.empty())
{
continue;
}
it=s.lower_bound(t1[i].y);
if(*it==t1[i].y)
{
cnt++;
s.erase(it);
}
else
{
if(it==s.begin())
{
continue;
}
else
{
s.erase(--it); //这里为什么要提前减一。 我们查询的是大于等于i的宽的第一个值。如果等于,最好,前面也判断了。 如果不等于,我们肯定要往前找,只要往前1个,那肯定就是小于i的宽了。 这是最优值,也就是保证可以覆盖情况下,最大的选择
cnt++;
}
}
}
printf("%d\n",cnt);
}
}
}
}
}
printf("%d\n",cnt);
}
}
阅读全文
0 0
- hdu4268 Alice and Bob
- HDU4268 Alice and Bob
- hdu4268 Alice and Bob
- hdu4268 Alice and Bob
- hdu4268 Alice and Bob,multiset
- HDU4268 Alice and Bob 【贪心】
- hdu4268 Alice and Bob 长春网络赛
- hdu4268 排序贪心Alice and Bob
- [STL][multiset] hdu4268 Alice and Bob
- hdu4268 Alice and Bob(贪心)
- HDU4268 Alice and Bob【贪心+SET】
- HDU4268 2012ACM长春赛区网络赛 Alice and Bob
- hdu4268 Alice and Bob(二维贪心的固定思想 STL的multiset的二分查找函数实现)
- Alice and Bob
- 2683. Alice and Bob
- 1798. Alice and Bob
- Alice, Bob and Chocolate
- A:Alice and Bob
- js—时间对象
- error LNK2019
- 2006 飞行员配对(二分图最大匹配)
- js正则替换字符串中的变量为变量的值
- redis命令
- hdu4268 Alice and Bob
- 河南省第十届大学生程序设计竞赛 年终奖金
- ubuntu中sublime安裝Package管理
- xcode 中使用 tesseract
- 原来--没有过不去的坎
- Zookeeper配置和使用
- Ubuntu-Linux 下命令行配置JAVA开发环境
- 《UNIX网络编程 卷1》之源码使用方法(for Ubuntu-10.04)
- Linux学习随笔