您的位置:首页 > 其它

HDU 4864 Task (贪心)

2014-07-23 20:15 309 查看

Task

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

Total Submission(s): 2154 Accepted Submission(s): 549


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

1 2
100 3
100 2
100 1


[align=left]Sample Output[/align]

1 50004


题意:

题目中告诉我们有n台机器和m个任务,每一台机器一天可以解决一个任务,一个任务一天也仅能由一台机器完成。对于它们之间的关系必须满足机器的运行时间比任务的工作时间长,以及机器的工作等级比任务的工作等级高。

当完成一个任务时,就可以得到 “500 * 任务时间 + 2 * 任务等级” 的金钱,问最多能完成多少任务同时得到最多的金钱。

思路:

首先这里的机器和任务是一对一的匹配,为了取得最多的金钱考虑到先去解决可以盈利较多的任务,那么这里就对任务和机器进行排序,采取先大后小的策略,由于任务时间占的权重较大(500 : 2),所以优先排序时间后排序等级。

对于每一个任务,每次取到最优的机器去做该任务。若采用全部重新遍历机器的方式,必定会超时(N * M)。

考虑到能完成前面任务(时间长,等级高的任务)的机器对于时间方面一定能达到要求。所以将这些能完成前面任务的机器全部标记下来,然后从中找出等级最小且满足等级要求的机器去完成任务。

/*************************************************************************
> File Name: hdu4864.cpp
> Author: Bslin
> Mail: Baoshenglin1994@gmail.com
> Created Time: 2014年07月23日 星期三 18时40分14秒
************************************************************************/

#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 100010

struct node {
int time, level;

bool operator < (const node &b) const {
if(time == b.time) {
return level > b.level;
}
return time > b.time;
}
} mach
, task
;
int vis[110];

int main(int argc, char *argv[]) {
freopen("in.txt", "r", stdin);
int n, m, i, j, k;
long long ans, num;
while(scanf("%d%d", &n, &m) != EOF) {
for (i = 0; i < n; ++i) {
scanf("%d%d", &mach[i].time, &mach[i].level);
}
for (i = 0; i < m; ++i) {
scanf("%d%d", &task[i].time, &task[i].level);
}
sort(mach, mach + n);
sort(task, task + m);
memset(vis, 0, sizeof(vis));
ans = 0;
num = 0;
for (i = 0, j = 0; i < m; ++i) {
while(j < n && mach[j].time >= task[i].time) {
vis[mach[j].level] ++;
j ++;
}
for (k = task[i].level; k <= 100; ++k) {
if(vis[k]) {
num ++;
vis[k] --;
ans += 500 * task[i].time + 2 * task[i].level;
break;
}
}
}
printf("%I64d %I64d\n", num, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: