HDU 4864 Task(经典贪心)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=4864
Task
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11382 Accepted Submission(s): 2782
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.
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.
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
Author
FZU
Source
Recommend
题目意思:
给你N个机器和M个任务, 每个任务有两个值花费时间x和难度y,
每个机器也有两个值最大工作时间x1和最大工作难度y1, 机器可以胜任某个工作的条件是x1>=x && y1>=y,
机器胜任一个工作可以拿到x*500+2*y的钱,现在问你怎么匹配才能使匹配数最大且钱数最多。
分析:
问你怎么匹配钱数最多
肯定是贪心的思想:
将任务按照权值1降序排序,权值1相同的按照权值二降序排序
将机器也同样是如此
在给任务选择机器的时候,在满足要求的机器中(机器的两个权值都大于任务的两个权值)选择权值1最给小的分配给该任务,这样保证了后面任务做的可能性增大
为什么是选择机器权值1最小的分配而不是权值2最小的呢?
因为权值1是乘以100,权值2是乘以1
肯定选择权值1嘛
code:
#include<stdio.h> #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <math.h> #include <cstdlib> #include <queue> #include<string.h> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define max_v 100005 typedef long long LL; struct node { int x,y; }p1[max_v],p2[max_v]; int cmp(node a,node b) { if(a.x==b.x) { return a.y>b.y; }else { return a.x>b.x; } } int main() { int n,m; while(cin>>n>>m) { for(int i=0;i<n;i++) cin>>p1[i].x>>p1[i].y; for(int i=0;i<m;i++) cin>>p2[i].x>>p2[i].y; sort(p1,p1+n,cmp); sort(p2,p2+m,cmp); int cnt=0; LL sum=0; int c[105]={0}; for(int i=0,j=0;i<m;i++) { while(j<n&&p1[j].x>=p2[i].x) { c[p1[j].y]++; j++; } for(int k=p2[i].y;k<=100;k++) { if(c[k]) { c[k]--; sum+=(p2[i].x*500+p2[i].y*2); cnt++; break; } } } printf("%d %I64d\n",cnt,sum); } return 0; }
心之所向,素履以往