HDU4864-Task

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

分析:

题意:
N个机器和M个任务, 每个任务有两个值花费时间x和难度y, 每个机器也有两个值最大工作时间x1和最大工作难度y1, 机器可以胜任某个工作的条件是x1>=x && y1>=y,机器胜任一个工作可以拿到x500+2y的钱,现在问你怎么匹配才能使匹配数最大且钱数最多。

解析:
贪心算法的比较好的题,我们按x的优先进行排序,降序升序皆可,因为x的权值远大于y的权值,而且y的最大值也才100,x加1的获利比y加100获得的利润都多。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring> 
#define N 100005

using namespace std;

struct node{
	int xi,yi;
};

node book[N],task[N];//book记录机器  task记录任务
int vist[105];//记录等级y有多少个
bool cmp(node a,node b)
{

	if(a.xi==b.xi)
		return a.yi<b.yi;
	return a.xi<b.xi;
}

int main()
{
	int n,m,num=0;
	long long sum=0; 
	while(~scanf("%d%d",&n,&m))
	{
		sum=0,num=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&book[i].xi,&book[i].yi);
		}
		sort(book+1,book+n+1,cmp);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&task[i].xi,&task[i].yi);
		}
		sort(task+1,task+m+1,cmp);
		for(int i=m,j=n;i>0;i--)
		{
			while(j>0&&book[j].xi>=task[i].xi)
			{
				vist[book[j].yi]++;
				j--;
			}
			for(int k=task[i].yi;k<=100;k++)
			{
				if(vist[k])
				{
					sum+=task[i].xi*500+task[i].yi*2;
					vist[k]--;
					num++;
					break; 
				}
			}
		}
		printf("%d %lld\n",num,sum);
		memset(vist,0,sizeof(vist));
	}
	return 0;
 } 
发布了46 篇原创文章 · 获赞 16 · 访问量 398

猜你喜欢

转载自blog.csdn.net/weixin_43357583/article/details/105139084