HDU 4864 Task

Task

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3273    Accepted Submission(s): 852



Problem Description
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.
 

Input
The 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.
 

Output
For 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
 

Author
FZU
 

Source
2014 Multi-University Training Contest 1


题目链接  :http://acm.hdu.edu.cn/showproblem.php?pid=4864


题目大意  :一共有n个任务,每个任务有个完成时间,和难度系数,一家公司有m台机器,每一个机器有一个等级和最长工作时间,每个机器一天最多只能完成一个任务,每个任务都只能被一个机器完成,如果机器的等级低于任务的难度系数或者机器的最长工作时间小于任务完成所需时间则该任务不能被该机器完成,每完成一个任务可以得到一定收益(根据题目给的公式),求公司收益的最大值


题目分析  :采用贪心策略,详见代码注释



#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int const MAX = 1e5 + 2;
struct Info
{
    int level;
    int cost;
}ma[MAX], task[MAX];

//按时间降序排序,若时间相同按等级降序排序
bool cmp(Info a, Info b)
{
    if(a.cost == b.cost)
        return a.level > b.level;
    return a.cost > b.cost;
}

int main()
{
    int n, m;
    int cnt;
    int maxml;
    long long gain;
    int re[101];
    while(scanf("%d %d", &n, &m) != EOF)
    {
        cnt = gain = 0;
        memset(re, 0, sizeof(re));
        for(int i = 0; i < n; i++)
            scanf("%d %d", &ma[i].cost, &ma[i].level);
        for(int i = 0; i < m; i++)
            scanf("%d %d", &task[i].cost, &task[i].level);
        sort(ma, ma + n, cmp);
        sort(task, task + m, cmp);
         //如果最后一个任务时间大于第一个机器时间则一个也不能完成
        if(ma[0].cost < task[m - 1].cost)
        {
            printf("0 0\n");
            continue;
        }
        for(int j = 0, i = 0; i < m; i++)
        {
            //对于每个任务,找到满足时间约束条件的机器并记录
            while(j < n && ma[j].cost >= task[i].cost)
            {
                //这里re数组记录的是具有相同等级的并且满足
                //当前任务时间约束的机器个数
                re[ma[j].level]++;
                j++;
            }
            for(int k = task[i].level; k <= 100; k++)
            {
                //如果能找到满足当前任务等级约束的机器,则该任务可以被完成
                if(re[k])
                {
                    cnt++;
                    gain += (500 * task[i].cost + 2 * task[i].level);
                    //完成后,将该机器去除
                    re[k]--;
                    break;
                }
            }
        }
        printf("%d %I64d\n", cnt, gain);
    }
}


你可能感兴趣的:(贪心,多校,hduoj)