1004(HDU4864) Task
题目描述:
Today the company has m tasks to complete. The ith task need ximinutes to complete. Meanwhile, this task has a difficulty level yi. Themachine whose level below this task’s level yi cannot complete this task. Ifthe company completes this task, they will get (500*xi+2*yi) dollars.
The company has n machines. Each machine has amaximum working time and a level. If the time for the task is more than themaximum working time of the machine, the machine can not complete this task.Each machine can only complete a task one day. Each task can only be completedby one machine.
The company hopes to maximize the number of thetasks which they can complete today. If there are multiple solutions, theyhopes to make the money maximum.
大意:
工厂有m任务和n个机器,每个任务有它所需的时间和等级,机器同样如此,每个机器只能做一件任务,完成每件任务的得到 500*时间+200*等级 这么多钱,问最多能收获多少钱?
官方题解:
基本思想是贪心。
对于价值c=500*xi+2*yi,yi最大影响100*2<500,所以就是求xi总和最大。可以先对机器和任务的时间从大到小排序。从最大时间的任务开始,找出满足任务时间要求的所有机器,从中找出等级最低且满足任务等级要求的机器匹配。依次对任务寻找满足要求的机器。
代码:
复杂度是 nlogn的,利用STL
/**
吉林大学
Jilin U
Statement: 以下代码完全由作者个人完成,不存在抄袭、套用。
Author: sinianluoye (JLU_LiChuang)
Date: 2014-07-22
Usage: 多校第一场
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#define ll long long
#define eps 1e-8
#define ms(x,y) (memset(x,y,sizeof(x)))
#define fr(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
struct task
{
int t,w;
};
bool cmp(task a,task b)
{
if(a.t!=b.t)return a.t>b.t;
else return a.w>b.w;
}
const int maxn=1e5+10;
task tas[maxn],mac[maxn];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
ms(used,0);
for(int i=1;i<=n;i++)
scanf("%d%d",&mac[i].t,&mac[i].w);
for(int i=1;i<=m;i++)
scanf("%d%d",&tas[i].t,&tas[i].w);
sort(mac+1,mac+n+1,cmp);
sort(tas+1,tas+m+1,cmp);
ll num=0,ans=0;
multiset<int>s;
for(int i=1,j=1;i<=m;i++)
{
while(j<=n&&mac[j].t>=tas[i].t)
s.insert(mac[j++].w);
multiset<int>::iterator p=s.lower_bound(tas[i].w);
if(p!=s.end())
{
num++;
ans+=500*tas[i].t+2*tas[i].w;
s.erase(p);
}
}
cout<<num<<' '<<ans<<endl;
}
return 0;
}
/*************copyright by sinianluoye (JLU_LiChuang)***********/