您的位置:首页 > 其它

HDU 4268 Alice and Bob 贪心STL O(nlogn)

2015-03-07 15:04 363 查看
B-AliceandBob
TimeLimit:5000MSMemoryLimit:32768KB64bitIOFormat:%I64d&%I64u

Description

  AliceandBob'sgameneverends.Today,theyintroduceanewgame.Inthisgame,bothofthemhaveNdifferentrectangularcardsrespectively.  
  AlicewantstousehiscardstocoverBob's.ThecardAcancoverthecardBiftheheightofAisnotsmallerthanBandthewidthofAisnotsmallerthanB.Asthebestprogrammer,youareaskedtocomputethemaximalnumberofBob'scardsthatAlicecancover.
Pleasepayattentionthateachcardcanbeusedonlyonceandthecardscannotberotated.

Input

  ThefirstlineoftheinputisanumberT(T<=40)whichmeansthenumberoftestcases.

  Foreachcase,thefirstlineisanumberNwhichmeansthe
numberofcardsthatAliceandBobhaverespectively.Eachofthe
followingN(N<=100,000)linescontainstwointegersh(h<=
1,000,000,000)andw(w<=1,000,000,000)whichmeanstheheightand
widthofAlice'scard,thenthefollowingNlinesmeansthatofBob's.

Output

  Foreachtestcase,outputananswerusingonelinewhichcontainsjustonenumber.

SampleInput

2
2
12
34
23
45
3
23
57
68
41
25
34

SampleOutput

12

题意:10W个数据,A有n张牌,B有n张牌,然后如果a.x>=b.x&&a.y>=b.y那么A就可以覆盖B
然后问你最多覆盖多少张
题解:暴力非常好想,O(n^2)跑一发就是,但是会T
那么我们就二分查找,或者用STL就好


multiset<int>myset;
multiset<int>::iteratorit;
constintmaxn=200000;
structnode
{
intx,y;
booloperator<(constnode&b)const
{
returnx<b.x;
}
}a[maxn],b[maxn];
intmain()
{
intsec,n;
scanf("%d",&sec);
for(intz=1;z<=sec;z++)
{
myset.clear();
scanf("%d",&n);
for(inti=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
for(inti=1;i<=n;i++)
scanf("%d%d",&b[i].x,&b[i].y);
sort(a+1,a+1+n);//按x从小到大排序
sort(b+1,b+1+n);//按x从小到大排序
intj=1;intans=0;//j是一个指向B数组位置的指针
for(inti=1;i<=n;i++)
{
while(j<=n&&b[j].x<=a[i].x)
{
myset.insert(b[j].y);
j++;
}
it=myset.upper_bound(a[i].y);
if(myset.size()>0&&it!=myset.begin())it--;
if(myset.size()>0&&(*it)<=a[i].y)
{
ans++;
myset.erase(it);
}
}
printf("%d\n",ans);
}
return0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: