hdu 4864

Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task’s level yi cannot complete this task. If the company completes this task, they will get (500*xi+2*yi) dollars.
The company has n machines. Each machine has a maximum working time and a level. If the time for the task is more than the maximum 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 completed by one machine.
The company hopes to maximize the number of the tasks which they can complete today. If there are multiple solutions, they hopes to make the money maximum.

InputThe input contains several test cases.
The first line contains two integers N and M. N is the number of the machines.M is the number of tasks(1 < =N <= 100000,1<=M<=100000).
The following N lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the maximum time the machine can work.yi is the level of the machine.
The following M lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the time we need to complete the task.yi is the level of the task.OutputFor each test case, output two integers, the maximum number of the tasks which the company can complete today and the money they will get.Sample Input

1 2
100 3
100 2
100 1

Sample Output

1 50004
这题学长那时候已经讲过了,因为x比y的影响大很多,所以肯定是按照x来贪心,但是一做题又傻眼了,因为数据量很大,不可能每次都遍历一遍机器和任务,所以我们想到,每次只从x入手,找到比任务x大的最后一个机器,然后因为y的范围很小,做一个y的哈希表
这样就能大大减少复杂度,就能过。这种思路在队列模拟的那道题出现过了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <utility>
#include <cstring>
using namespace std;
pair<int,int> mm[100003];
pair<int,int> t[100003];
int s[150];
int main()
{
    int n,m;
    while(cin>>n>>m)
{
    memset(s,0,sizeof(s));
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&mm[i].first,&mm[i].second);
    }
    for(int i=0;i<m;i++)
    scanf("%d%d",&t[i].first,&t[i].second);
    sort(mm,mm+n,greater<pair<int,int> >());
    sort(t,t+m,greater<pair<int,int> >());
    long long ans=0;
    int num=0;
    int j=0;
    for(int i=0;i<m;i++)
    {
        while(j<n&&mm[j].first>=t[i].first)
        {
            s[mm[j].second]++;
            j++;
        } 
        for(int k=t[i].second;k<=100;k++)
        {
            if(s[k]>0)
            {
                num++;
                s[k]--;
                ans+=500*t[i].first+2*t[i].second;
                break;
            }
        }
    }
    cout<<num<<" "<<ans<<endl;
}
}


猜你喜欢

转载自www.cnblogs.com/coolwx/p/11146009.html