HDU 4864 Task

来源:互联网 发布:spss数据标准化公式 编辑:程序博客网 时间:2024/06/06 19:24

题目链接:点击打开链接

贪心

Task

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7206    Accepted Submission(s): 1897


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.
 

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 2100 3100 2100 1
 

Sample Output
1 50004

题目意思是有n台机器,需要完成 m个任务,每台机器有运行时间和等级,每个任务同样有运行时间和等级,机器只能完成等级比自己低的任务,问最后机器能完成多少任务,且能赚多少钱。

按照时间给机器和任务都进行升序排序,遍历任务,用数组记录时间大于这个任务且等级大于这个任务的机器,用数组下标记录任务的等级,把所有合格的机器遍历一遍,找到级最低的机器(也就是再次遍历一遍数组),用这台机去完成任务。最后输出结果,由于数据量较小,所以没有超时。


代码实现:

#include<iostream>#include<algorithm>#include<cstring>using namespace std;struct node{int t;int l;};node p[100005],b[100005];int cmp(node a,node b){if(a.t==b.t)return a.l>b.l;return a.t>b.t;}int main(){int m,n,i,j,k;int v[100005];long long sum,count;while(cin>>n>>m){sum=count=0;for(i=0;i<n;i++)cin>>b[i].t>>b[i].l;for(i=0;i<m;i++)cin>>p[i].t>>p[i].l;sort(p,p+m,cmp);sort(b,b+n,cmp);memset(v,0,sizeof(v));k=0;for(i=0;i<m;i++){while(k<n&&b[k].t>=p[i].t) {v[b[k++].l]++;}for(j=p[i].l;j<=100;j++){if(v[j]){count++;sum+=500*p[i].t+2*p[i].l;v[j]--;break;}}}cout<<count<<' '<<sum<<endl;}return 0;}


原创粉丝点击