Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16172 Accepted Submission(s): 4032
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
Author
FZU
Source
2014 Multi-University Training Contest 1
翻译:
n台机器,m个任务。每个机器最多做一个任务。每个任务最多被一台机器做。
每个任务和机器都有两个属性:x,y。只有当机器的两个属性都大于等于任务的时候,机器才可以接受这个任务。
一个任务完成可以挣500x+2y的钱数。
问在任务完成数最多的情况下,最多能挣到多少钱?
分析
思路:
贪心。把任务和机器分别按照两个属性从大到小排序。x是第一优先级,y第二
遍历。对于每个任务,所有机器的x满足条件的,就把机器的y值作为key放入到map中,如果map里已经存在该值,则++。当发现下个机器的x不满足或者机器遍历完后,从map中找到键值大于等于机器y值最小的让这个机器完成该任务。
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main{
static final int N=(int)1e5+7;
public static void main(String[] args)throws IOException {
Scanner sc=new Scanner(System.in);
int n,x,y;
node machine[]=new node[N];
node task []=new node[N];
for(int i=1;i<N;i++) {
machine[i]=new node();
task[i]=new node();
}
int f[]=new int [105];
while(sc.hasNext()) {
n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++) {
x=sc.nextInt();y=sc.nextInt();
machine[i].set(x, y);
}
for(int i=1;i<=m;i++) {
x=sc.nextInt();y=sc.nextInt();
task[i].set(x, y);
}
Arrays.sort(machine,1,n+1);
Arrays.sort(task,1,m+1);
Arrays.fill(f, 0);
int num=0;//匹配的数量
long sum=0;//注意数据范围
int j=1;
for(int i=1;i<=m;i++) {
while(j<=n&&machine[j].x>=task[i].x) f[machine[j++].y]++;
for(int k=task[i].y;k<=100;k++) {
if(f[k]>0) {
num++;
sum+=500*task[i].x+2*task[i].y;
f[k]--;
break;
}
}
}
System.out.println(num+" "+sum);
}
}
static class node implements Comparable<node>{
int x,y;
public node() {}
void set(int x,int y) {
this.x=x;this.y=y;
}
public int compareTo(node o) {
if(x!=o.x) return o.x-x;
return o.y-y;
}
}
}