Greedy -- hdu4864

Source: Internet
Author: User

Corresponding HDU question: Click to open the link

 

Task

Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3427 accepted submission (s): 887


Problem descriptiontoday 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.


 

Inputthe 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.


 

Outputfor 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

 

First of all, for any task, because the final money is 500 * x + 2 * y, it must have a high priority for X to get the most money, when X is the same, compare y

Then, the question is to ensure that the maximum number of tasks is completed, and the maximum amount of money is required. In terms of the number of tasks completed, the question is obviously that a machine can only correspond to one task. Therefore, if a task only occupies a machine that is just enough for its own use, it is definitely the best, because even if it is replaced with him, that is, 1 for 1, there is no difference, of course, if he occupies a machine that is much better than himself, there may be problems, because there may be more advanced tasks that will be excluded, so that two tasks can be completed and only one task can be completed, therefore, we only need to ensure that this task meets the requirements of this machine.

Therefore, we sort the tasks in descending order based on the maximum money principle and place them on the machine that is most suitable for them (the machines are sorted according to the same rules, and then divide them into two points ). If it can be put, it will be put. If it cannot be put, it will be thrown away. According to the above analysis, the result obtained in this way must be optimal .. In addition, there are many possibilities, because I put the most value, the money will be the best.

 

#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;}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.