您的位置:首页 > 其它

hdu4268 Alice and Bob

2013-07-25 17:21 316 查看

Alice and Bob

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1800 Accepted Submission(s): 645


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, output an answer using one line which contains just one number.

[align=left]Sample Input[/align]

2
2
1 2
3 4
2 3
4 5
3
2 3
5 7
6 8
4 1
2 5
3 4

[align=left]Sample Output[/align]

1
2唉,这题不说了,打比赛,花了五个小时也没做出来,打队友给坑了,下来后思考了很长时间,才发现,我思路有问题,这里,因为,不是每两个牌都有确定的大小关系,因为有可以是一个宽度大,而另一个是长度大,这样一大一小的情况,让人伤脑筋,其实,可以这样,我们先按长,再按宽排,这样排安序之后,我们用一个set把所有的第二个人的牌存进,存入的是宽,因为,我们是先按长度从小到大的,这样我们就能保证,在前面的一定,是长小的,我们再用一个upper_bound找出第一个比这个数的宽要大的—,再向前移一位,那么就找到了一个最大的能盖住的长方形,注意,一定是要最大的,如果不是最大的,在最后就可可能后面的取不到的情况,这也就是一个贪心的做法!好了,到这里,这题就可以a了!注意要用一个set啊什么的,加快查询的速才行!
#include<iostream>
#include <stdio.h>
#include <algorithm>
#include <set>
using namespace std;
struct node {
__int64 h,w;
int n;
}l[200005];
multiset<int >myset;
bool cmp(node a,node b)
{
if(a.h!=b.h)
return a.h<b.h;
else{
if(a.w!=b.w)
return a.w<b.w;
else
{
return a.n<b.n;
}
}
}
int main ()
{
int tcase,n2,i,re,n;
multiset<int >::iterator p;
scanf("%d",&tcase);
while(tcase--)
{
myset.clear();
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%I64d%I64d",&l[i].h,&l[i].w);
l[i].n=1;
}
n2=2*n;
for(;i<n2;i++)
{
scanf("%I64d%I64d",&l[i].h,&l[i].w);
l[i].n=0;
}
sort(l,l+n2,cmp);
re=0;
for(i=0;i<n2;i++)
{
if(!l[i].n)
{
myset.insert(l[i].w);
}
else{
if(!myset.empty()&&*myset.begin()<=l[i].w)
{
p=myset.upper_bound(l[i].w);
p--;//找到小一个或相等的
re++;
myset.erase(p);

}

}
}
printf("%d\n",re);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu4268 Alice and Bo