有n个机器,m个任务,每个任务只能交给一个机器做,每个机器只能做一个任务,任务需要同时满足时间<=x, 等级>=y
基本思想是贪心。
对于价值c=500*xi+2*yi,yi最大影响100*2<500,所以就是求xi总和最大。可以先对机器和任务的时间从大到小排序。从最大时间的任务开始,找出满足任务时间要求的所有机器,从中找出等级最低且满足任务等级要求的机器匹配。依次对任务寻找满足要求的机器。
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "algorithm"
using namespace std;
struct node
{
int x,y;
}a[100010],b[100010];
bool cmp(node a,node b)
{
if (a.x==b.x) return a.y>b.y;
else return a.x>b.x;
}
int hash[101];
int main()
{
int n,m,i,j,ans1,l;
__int64 ans2;
while (scanf("%d%d",&n,&m)!=EOF)
{
for (i=0;i<n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
for (i=0;i<m;i++)
scanf("%d%d",&b[i].x,&b[i].y);
sort(a,a+n,cmp);
sort(b,b+m,cmp);
memset(hash,0,sizeof(hash));
ans1=ans2=0;
j=0;
for (i=0;i<m;i++)
{
while(j<n && a[j].x>=b[i].x)
hash[a[j++].y]++;
for (l=b[i].y;l<=100;l++)
if (hash[l]!=0)
{
hash[l]--;
ans1++;
ans2+=500*b[i].x+2*b[i].y;
break;
}
}
printf("%d %I64d\n",ans1,ans2);
}
return 0;
}