poj 3614 贪心解决最大匹配

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值