对应HDU题目:点击打开链接
Task
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3427 Accepted Submission(s): 887
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 2 100 3 100 2 100 1
Sample Output
1 50004
首先,对于任何的task,因为最后的money是 500*x+2*y,所以,在得到最多money方面,肯定是x的优先级高,x相同的时候再比较y
然后题目是要求 在保证最多任务完成量的前提下,money最多,在任务完成量方面,首先,题目一个很明显的说法是一个机器只能跟一个task对应,所以,一个task如果只是占用了刚好够自己用的机器,那绝对就是最优的,因为就算替换他,也就是1换1,没有任何区别,当然,如果他占用了比自己好很多的机器就可能有问题了,因为可能有更高级的任务会被排开,这样原本可能可以完成2件 就只能完成1件,所以,我们只要保证这个task是刚好满足这个机器,即可
于是,我们把task按照money最大的原则降序排序,把他们依次放到最适合他们的机器上(机器照同样的规则排完序,然后二分即可)。如果能放就放,不能放就扔了。根据上面的分析,这样求出来的结果必然是最优的。。而且在存在多种可能的情况下,因为我是按最大价值放的,得到的money也必定是最优的
#include<cstdio> #include<cstdlib> #include<cmath> #include<map> #include<queue> #include<stack> #include<vector> #include<algorithm> #include<cstring> #include<string> #include<iostream> const int MAXN=100000+10; using namespace std; int num[100]; struct node { int x,y; }mac[MAXN],task[MAXN]; bool cmp(node m1, node m2) { if(m1.x!=m2.x) return m1.x>m2.x; return m1.y>m2.y; } int main() { //freopen("in.txt","r",stdin); int n,m; while(scanf("%d%d", &n,&m)==2) { memset(num,0,sizeof(num)); int i,j; for(i=1; i<=n; i++){ scanf("%d%d", &mac[i].x, &mac[i].y); } for(i=1; i<=m; i++){ scanf("%d%d", &task[i].x, &task[i].y); } sort(mac+1, mac+n+1, cmp); sort(task+1, task+m+1, cmp); int cnt=0; long long sum=0; for(i=1,j=1; i<=m; i++){ while(task[i].x<=mac[j].x && j<=n) { num[mac[j].y]++; j++; } for(int l=task[i].y; l<=100; l++){ if(num[l]){ sum+=(long long)(500*task[i].x+2*task[i].y); cnt++; num[l]--; break; } } } printf("%d %I64d\n",cnt, sum); } return 0; }
贪心——HDU4864