http://acm.pku.edu.cn/JudgeOnline/problem?id=3614
解题报告提示:
We first sort the bottles by SPF rating and the cows by minSPF rating. We then scan the bottles in order and maintain a min-heap on the cows (keyed on maxSPF) with minSPF lower than the SPF of our current bottle. For each bottle B in order, we match it with the minimum cow from the heap (after first removing any cows from the heap having maxSPF less than the SPF of B).
#include<iostream>#include <algorithm>
using namespace std;struct Cow
{
int ls,us;
}cow[3000],cow0[3000];
int lotion[1005],nc;
int cmp(Cow c1,Cow c2)
{
return c1.ls<c2.ls;
}
void heapadd(Cow c0)
{
cow0[nc++]=c0;
int j=nc-1;
while(j>0)
{
if(cow0[j].us>=cow0[(j-1)/2].us)
break;
Cow tc=cow0[j];
cow0[j]=cow0[(j-1)/2];
cow0[(j-1)/2]=tc;
j=(j-1)/2;
}
}int heapdelete()
{
Cow tc=cow0[0];
cow0[0]=cow0[nc-1];
nc--;
int j=0;
while(2*j+1<nc)
{
int t=2*j+1;
if(2*j+2<nc&&cow0[2*j+2].us<cow0[t].us)
t=2*j+2;
if(cow0[j].us<=cow0[t].us)
break;
Cow tc=cow0[j];
cow0[j]=cow0[t];
cow0[t]=tc;
j=t;
}
return tc.us;
}int main()
{
int c,l,i,spf,num;
scanf("%d%d",&c,&l);
memset(lotion,0,sizeof(lotion));
for(i=0;i<c;i++)
scanf("%d%d",&cow[i].ls,&cow[i].us);
sort(cow,cow+c,cmp);
for(i=0;i<l;i++)
{
scanf("%d%d",&spf,&num);
lotion[spf]+=num;
}
int t=0;
int res=0;
nc=0;
int cc=0;
for(i=1;i<1001;i++)
{
while(cc<c&&cow[cc].ls==i)
{
heapadd(cow[cc]);
cc++;
}
while(lotion[i])
{
if(nc==0)
break;
if(heapdelete()>=i){
lotion[i]--;
res++;
}
}
}
printf("%d\n",res);
return 0;
}