首页 > 代码库 > HDU4864 Task

HDU4864 Task

Task

 

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1997 Accepted Submission(s): 517



Problem Description
Today the company has m tasks to complete. Theith task need xi minutes to complete. Meanwhile, this task has a difficultylevel yi. The machine whose level below this task’s level yi cannot completethis task. If the 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.


Input
The input contains several test cases. 
The first line contains two integers N and M. Nis the number of the machines.M is the number of tasks(1 < =N <=100000,1<=M<=100000).
The following N lines each contains two integersxi(0<xi<1440),yi(0=<yi<=100).xi is the maximum time the machine canwork.yi is the level of the machine.
The following M lines each contains two integersxi(0<xi<1440),yi(0=<yi<=100).xi is the time we need to complete thetask.yi is the level of the task.


Output
For each test case, output two integers, themaximum number of the tasks which the company can complete today and the moneythey will get.


Sample Input
1 2
100 3
100 2
100 1


Sample Output
1 50004


Author
FZU

 

题意:给n个机器和m个任务,机器和任务都有时间和等级,只能机器的等级和时间都大于这个任务机器才能完成这个任务(一台机器最多只能完成一个额任务),问最多能完成几个任务,任务相同输出最大的价值。

这题用贪心做,虽然想到了贪心但是贪心策略错了最后还是没能AC,因为任务的价值实际上就只是受时间的影响(2*100都不大于500,所以时间差1排序就不用看等级了),所以任务和机器都按时间从大到小排序,然后用任务区匹配机器,找出满足时间要求里等级最小的机器与任务匹配,这样做实际上都只遍历了一遍机器和一遍任务(遍历多次机器就超时了)。

 

#include<cstdio>#include<cstring>#include<algorithm>#define LL __int64using namespace std;const int MAXN=100010;int vis[MAXN];struct machine{    LL time,level;}m[MAXN];struct task{    LL time,level;    LL d;}t[MAXN];bool cmp1(machine a,machine b){    if(a.time==b.time)        return a.level>b.level;    return a.time>b.time;}bool cmp2(task a,task b){    if(a.time==b.time)        return a.level>b.level;    return a.time>b.time;}int main(){    int n,k,i,j,p;    while(scanf("%d%d",&n,&k)==2)    {        for(i=0;i<n;i++)            scanf("%I64d%I64d",&m[i].time,&m[i].level);        sort(m,m+n,cmp1);        for(i=0;i<k;i++)        {            scanf("%I64d%I64d",&t[i].time,&t[i].level);            t[i].d=500*t[i].time+2*t[i].level;        }        sort(t,t+k,cmp2);        int cnt=0;        LL ans=0;        memset(vis,0,sizeof(vis));        for(i=0,j=0;i<k;i++)        {            while(m[j].time>=t[i].time&&j<n)            {                vis[m[j].level]++;  //机器可能具有相同的等级                j++;            }            for(p=t[i].level;p<=100;p++)            {                if(vis[p])                {                    cnt++;                    ans+=t[i].d;                    vis[p]--;                    break;                }            }        }        printf("%d %I64d\n",cnt,ans);    }    return 0;}