Task(贪心)
http://acm.hdu.edu.cn/showproblem.php?pid=4864
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 (500xi+2yi) 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
Source
2014 Multi-University Training Contest 1
题意:
有n个机器,m个任务。每个机器至多能完成一个任务。对于每个机器,有一个最大运行时间xi和等级yi。对于每个任务,也有一个运行时间xj和等级yj。只有当xi>=xj且yi>=yj的时候,机器i才可以完成任务j,并获得500xj+2yj的钱。问最多完成几个任务,当出现多种情况时,输出获得金钱最多的情况。
思路:
注意看范围 xi(0<xi<1440),yi(0=<yi<=100).通过这个范围可知,时间才是最关键的因素,所以按照时间从大到小排序,如果时间相同,这时按照等级从大到小排序。使用这个排序规则来排序机器和任务。
这时可以通过遍历一遍任务,找出所有xi,使(xi >= xj)。然后再从找出的xi中找到yi最小的一个作为完成这个任务的机器。因为后面的时间肯定满足,但是等级可能不满足,所以你要从中选择等级最小的作为本次的机器。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
struct node{
int x;
int y;
}a[maxn],b[maxn];
bool cmp(node q1, node q2)
{
if(q1.x == q2.x) {
return q1.y > q2.y;
} else
return q1.x > q2.x;
}
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m)) {
for(int i = 0; i < n; i++)
scanf("%d%d", &a[i].x, &a[i].y);
for(int i = 0; i < m; i++)
scanf("%d%d", &b[i].x, &b[i].y);
sort(b, b+m, cmp);
sort(a, a+n, cmp);
int i = 0;
int ans[maxn];
memset(ans, 0, sizeof(ans));
int count = 0;
long long sum = 0;
for(int j = 0; j < m; j++) {
while(a[i].x >= b[j].x && i < n) {
ans[a[i].y]++;
i++;
}
for(int k = b[j].y ; k <= 100 ;k++) {
if(ans[k]) {
ans[k]--;
count++;
sum += (long long)(500*b[j].x + 2 * b[j].y);
break;
}
}
}
printf("%d %lld\n",count,sum);
}
return 0;
}