您的位置:首页 > 其它

HDU-4864 Task(贪心)

2016-05-02 20:58 369 查看


Task

http://acm.hdu.edu.cn/showproblem.php?pid=4864

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



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


题目大意:有n台机器,每台机器都有x值和y值,有m个任务,每个任务都有x值和y值,每完成一个任务,会获得500*x+2y(任务的x值和y值)元,机器和任务都最多使用一次,求最多能获得多少元?

大致思路:很容易就能想到排序贪心,但是贪心还有技巧,否则就不能取得最大值

首先x值大于任务的机器均可以使用一次,若存在x大于任务的机器,则该任务必定会执行,因为500*x不会小于后面任务所得,则难点就在如何在众多符合要求的机器中选择

此时再考虑y值,由于未被选择的机器可以在后面的任务中使用,所以为了得到最优解,当前任务必定匹配y值大于它的y值且最接近的那一台机器

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN=100005;

struct Node {
int x,y;

bool operator < (const Node& a) const {
return x<a.x||(x==a.x&&y<a.y);
}
}mach[MAXN],task[MAXN];

int n,m,cnt,y[105];
long long ans;

int main() {
while(scanf("%d%d",&n,&m)==2) {
for(int i=0;i<n;++i) {
scanf("%d%d",&mach[i].x,&mach[i].y);
}
for(int i=0;i<m;++i) {
scanf("%d%d",&task[i].x,&task[i].y);
}
sort(mach,mach+n);
sort(task,task+m);
ans=cnt=0;
memset(y,0,sizeof(y));
for(int i=m-1,j=n-1;i>=0;--i) {
while(j>=0&&mach[j].x>=task[i].x) {//统计x值大于任务的机器的y值出现的次数
++y[mach[j].y];
--j;
}
for(int k=task[i].y;k<=100;++k) {//寻找第一个大于机器y值的y值
if(y[k]!=0) {
++cnt;
ans+=500*task[i].x+2*task[i].y;
--y[k];
break;
}
}
}
printf("%d %I64d\n",cnt,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: