题目为:
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.
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
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.
在这里给出一组输入。例如:
1 2
100 3
100 2
100 1
在这里给出相应的输出。例如:
1 50004
简单总结解决这个问题思路:
先把机器和任务结构体数组排个序,先优先按xi从大到小,在考虑yi.因为xi要乘以500,比重大。刚开始进入误区,把排好序的任务第一个优先考虑,直接在排好序的机器中找,只要符合条件(机器的时间和等级都要小于任务的),这是一个考虑不周全的。因为还要让任务完成的数量还要最多,这也是一个得到最优解的要求。所以需要在思考一下。把任务比重大的给它安排时,不能随便的安排一个机器,需要先在时间条件满足情况下,选择等级符合条件,且等级最小的这些机器,则会等级大的机器出来,可以安排更多的任务。从而使完成任务数量最多。在这个过程中有2个最优子的过程
代码如下:
#include
using namespace std;
typedef struct good
{
int x,y;
}good;
good mach[100005],task[100005];
bool comp(good a,good b)
{
if(a.x==b.x)
return a.y>b.y;
return a.x>b.x;
}
int main()
{
int n,m,num,x,y,level[105];
long sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(level,0,sizeof(level));//level用来符合条件的机器位置
for(int i=0;i<n;i++)
scanf("%d%d",&mach[i].x,&mach[i].y);
for(int i=0;i<m;i++)
scanf("%d%d",&task[i].x,&task[i].y);
sort(mach,mach+n,comp);
sort(task,task+m,comp);
num=0; sum=0; //完成任务个数,收益
int j=0;
for(int i=0;i<m;i++)
{
while(j<n&&mach[j].x>=task[i].x) level[mach[j++].y]++;//在机器中找先找时间大于任务时间的,把这些机器位置记录下来
for(int lev=task[i].y; lev<=100;lev++)//在上面记录机器中,再找>=任务等级 ,选机器任务等级符合要求最小的那个
if(level[lev])
{
num++;
sum+=500*task[i].x+2*task[i].y;
level[lev]--;
break;
}
}
cout<<num<<' '<<sum<<endl;
}
}
运行结果:
1 2
100 3
100 2
100 1
1 50004
//可以继续输入