Task / HDU - 4864

Posted little-turtle--qjy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Task / 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.

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

题意

多组数据
有N个机器与M个任务,每个机器或者任务都有两个属性(x,y)
(N <= 100000, M <= 100000, 0 < x <1440, 0<=y <= 100)
机器的x表示这个机器的最长工作时间, y表示机器的等级
任务的x表示完成这个任务需要花费的时间,y表示任务的等级
每个机器只能做一个任务,机器的等级必须大于等于任务的等级
每完成一个任务可以获得500x+2y的价值(x,y是任务的x,y)
请问最多可以完成多少个任务以及在此基础上最多可以获得多少的价值

题解

按x(时间)为第一关键字, y(等级)为第二关键字从大到小排序
       (时间的从大到小 和 等级的从大到小)
接着从第一个任务开始找,
1: 如果某台机器的时间符合这个任务,则标记一下其等级 表示可以用
2: 从这个任务的等级开始往上找,找到的第一台机器就使用这台机器
3: 更新一下数据
贪心:贪 时间符合 并且等级最小的机器

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;

int n, m;
int level[110];
long long cnt, sum;

struct node{
    int x, y;
}robot[N], task[N];

bool cmp(node a, node b){
    return a.x > b.x || (a.x == b.x && a.y > b.y);
}

int main(){
    while(scanf("%d%d", &n, &m) != EOF){
        for(int i = 0; i < n; i++){
            scanf("%d%d", &robot[i].x, &robot[i].y);
        }
        for(int i = 0; i < m; i++){
            scanf("%d%d", &task[i].x, &task[i].y);
        }
        sort(robot, robot+n, cmp);//排序
        sort(task, task+m, cmp);//排序
        cnt = 0, sum = 0;
        memset(level, 0, sizeof(level));
        for(int i = 0, j = 0; i < m; i++){
            while(j < n && robot[j].x >= task[i].x){//找所有时间符合的机器
                level[robot[j].y]++;//这个等级的机器有多少加多少
                j++;
            }
            for(int k = task[i].y; k <= 100; k++){//找等级最小的一台机器
                if(level[k]){
                    level[k]--;//这台没啦
                    cnt++;
                    sum += 500 * task[i].x + 2 * task[i].y;
                    break;
                }
            }
        }
        printf("%lld %lld
", cnt, sum);
    }
    return 0;
}

PS

为什么在新的循环中,j不再重新从0开始,而是延续之前的值
因为根据我们的排序可这,任务的时间是越来越小的或者相等的
所以上一次循环(也就是上一个任务),满足条件的机器再这一个任务也肯定满足
所以不需要重新初始化

以上是关于Task / HDU - 4864的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4864 Task(经典贪心)

Task / HDU - 4864

HDU4864:Task(贪心)

HDU 4864 Task

HDOJ4864 Task 题解报告

Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题