Task(二维关键字不同且有向的贪心思想(有坑点,反向求会wa。。))

来源:互联网 发布:网络扫描技术揭秘 下载 编辑:程序博客网 时间:2024/05/23 19:11


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.
 

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
 

Author
FZU
 

Source
2014 Multi-University Training Contest 1
 


题意:N台机器,M个任务,机器和任务分别有一个time值,和level值。每台机器上最多只能运行一个任务,而且机器的time值和level值要分别大于等于该任务的值才能做该任务。完成一个任务会获得(500*time+2*level)的价值,求对于给出的机器和任务,能获得的最大价值。

编程思想:贪心。题目很特别的给出了获得金钱的计算公式为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 1000010using namespace std;struct node{int xi;//时间 int yi;//級別 bool operator < (const node &b)const{if(xi!=b.xi)return xi>b.xi;elsereturn 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;}


0 0
原创粉丝点击