1. 程式人生 > >HDU 4864 Task(經典貪心)

HDU 4864 Task(經典貪心)

傳送門:

Task

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


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 Author FZU Source Recommend 題目意思: 給你N個機器和M個任務, 每個任務有兩個值花費時間x和難度y, 每個機器也有兩個值最大工作時間x1和最大工作難度y1, 機器可以勝任某個工作的條件是x1>=x && y1>=y, 機器勝任一個工作可以拿到x*500+2*y的錢,現在問你怎麼匹配才能使匹配數最大且錢數最多。 分析: 問你怎麼匹配錢數最多 肯定是貪心的思想: 將任務按照權值1降序排序,權值1相同的按照權值二降序排序 將機器也同樣是如此 在給任務選擇機器的時候,在滿足要求的機器中(機器的兩個權值都大於任務的兩個權值)選擇權值1最給小的分配給該任務,這樣保證了後面任務做的可能性增大 為什麼是選擇機器權值1最小的分配而不是權值2最小的呢? 因為權值1是乘以100,權值2是乘以1 肯定選擇權值1嘛 code:
#include<stdio.h>
#include 
<iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <math.h> #include <cstdlib> #include <queue> #include<string.h> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define max_v 100005 typedef
long long LL; struct node { int x,y; }p1[max_v],p2[max_v]; int cmp(node a,node b) { if(a.x==b.x) { return a.y>b.y; }else { return a.x>b.x; } } int main() { int n,m; while(cin>>n>>m) { for(int i=0;i<n;i++) cin
>>p1[i].x>>p1[i].y; for(int i=0;i<m;i++) cin>>p2[i].x>>p2[i].y; sort(p1,p1+n,cmp); sort(p2,p2+m,cmp); int cnt=0; LL sum=0; int c[105]={0}; for(int i=0,j=0;i<m;i++) { while(j<n&&p1[j].x>=p2[i].x) { c[p1[j].y]++; j++; } for(int k=p2[i].y;k<=100;k++) { if(c[k]) { c[k]--; sum+=(p2[i].x*500+p2[i].y*2); cnt++; break; } } } printf("%d %I64d\n",cnt,sum); } return 0; }