D - Task
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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.
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
对于每一个任务:找出可以完成这个任务的所有机器中,level最低的那一个来完成。
因为这些机器的时间满足这个任务,必然也满足这个任务后面所有的任务,但是可能后面的任务的level非常大
所以我们要尽量保留level大的机器以备后面任务的使用。
将所有满足机器运行时间>=任务时间的加入数组,然后选满足完成任务的最小等级的机器去完成任务。这样的巧妙之处在于后面的加进来的任务必定可以被先前加进来的机所完成。因为任务是按时间降序排列的。
对我来说是难题。
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
typedef int ll;
const int MAX=100005;
struct node
{
ll x,d;
bool used;
};
bool cmp(node a,node b)
{
if (a.x==b.x)
return a.d>b.d;
else return a.x>b.x;
}
ll N,M;
node machine[MAX];
node task[MAX];
int cnt[105];
int main()
{
while (cin>>N>>M)
{
int ans=0;
long long money=0;
memset(cnt,0,sizeof(cnt));
for (int i=0;i<N;i++)
{
cin>>machine[i].x>>machine[i].d;
machine[i].used=false;
}
for (int i=0;i<M;i++)
{
cin>>task[i].x>>task[i].d;
task[i].used=false;
}
sort(machine,machine+N,cmp);
sort(task,task+M,cmp);
for (int i=0,j=0;i<M;i++)
{
while (j<N&&machine[j].x>=task[i].x)
{
cnt[machine[j].d]++;
j++;
}
for (int k=task[i].d;k<=100;k++)
{
if (cnt[k])
{
cnt[k]--;
money+=task[i].x*500+task[i].d*2;
ans++;
break;
}
}
}
cout<<ans<<' '<<money<<endl;
}
}