您的位置:首页 > 其它

hdu 4864 Task 贪心

2014-07-23 11:22 453 查看

Task

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


[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

[align=left]Author[/align]
FZU

[align=left]Source[/align]
2014 Multi-University Training Contest 1

将任务和机器以x从大到小,x相等时y从大到小的顺序排序。遍历任务,对每个任务找到满足条件的y值最小的机器,作为完成这个任务的机器。
当时我们的想法是对每个机器贪心,以从小到大的顺序排序,对每个机器找当前能完成的价值最大的任务。
这个想法是不对的,看一组反例:
2 3
1126 77
1204 23

1032 4
977 48
944 22
最优的策略应该是
(1126,77)这个机器完成(977,48)这个任务,(1204,23)完成(1032,4)这个任务。
但是对机器贪心,结果是(1126,77)完成(1032,4),(1204,23)完成(944,22)。

因为这里y的范围是0到100,可以用计数排序。

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long

using namespace std;
const int maxn = 100005;
struct Node
{
int x, y, id;
}mc[maxn], ts[maxn];
int cnt[maxn];
bool cmp(Node a, Node b)
{
if (a.x!=b.x) return a.x>b.x;
return a.y>b.y;
}
int main()
{
int n, m;
while (scanf("%d%d", &n, &m)==2) {
for (int i=0; i<n; i++) {
scanf("%d%d", &mc[i].x, &mc[i].y);
}
for (int i=0; i<m; i++) {
scanf("%d%d", &ts[i].x, &ts[i].y);
}
sort(ts, ts+m, cmp);
sort(mc, mc+n, cmp);
memset(cnt, 0, sizeof(cnt));
int num=0;
ll ans=0;
for (int i=0, j=0; i<m; i++) {
while (j<n&&mc[j].x>=ts[i].x) {
cnt[mc[j].y]++;
j++;
}
for (int k=ts[i].y; k<=100; k++) {
if (cnt[k]) {
cnt[k]--;
num++;
ans+=500*ts[i].x+2*ts[i].y;
break;
}
}
}
printf("%d %I64d\n", num, ans);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: