Link: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): 4606 Accepted Submission(s): 1207
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
编程思想:贪心。题目很特别的给出了获得金钱的计算公式为500*x+2*y,y<=100,也就是说y怎么样也没有x变化1价值增加的多,所以我们要优先保护x,所以在二级排序的时候先以x,再y,这样就可以在保证个数最大的情况下价值最大了,整个过程还是简单贪心,所以可以按任务时间从大到小排序,然后每个任务找出大于等于该任务难度且与难度最接近(sett.lower_bound(task[i].yi)函数)的机器完成该任务。
AC code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<set>
#define LL long long
#define MAXN 1000010
using namespace std;
struct node{
int xi;//时间
int yi;//級別
bool operator < (const node &b)const
{
if(xi!=b.xi)
return xi>b.xi;
else
return yi>b.yi;
}
}machine[MAXN],task[MAXN];
multiset<int>sett;
multiset<int>::iterator it;
LL ans;
int cnt;
int main()
{
// freopen("D:\\in.txt","r",stdin);
int T,i,j,n,m;
while(~scanf("%d%d",&n,&m))
{
sett.clear();
for(i=1;i<=n;i++)
{
scanf("%d%d",&machine[i].xi,&machine[i].yi);
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&task[i].xi,&task[i].yi);
}
sort(machine+1,machine+n+1);
sort(task+1,task+m+1);
j=1;
ans=0;
cnt=0;
for(i=1;i<=m;i++)
{
while(j<=n&&machine[j].xi>=task[i].xi)
{
sett.insert(machine[j].yi);
j++;
}
if(sett.empty())
{
continue;
}
it=sett.lower_bound(task[i].yi);
if(it!=sett.end())
{
ans+=500*(task[i].xi)+2*(task[i].yi);
cnt++;
sett.erase(it);
}
}
printf("%d %I64d\n",cnt,ans);
}
return 0;
}