HDU 4864 Task(2014 Multi-University Training Contest 1) 贪心算法

题目链接:Task

Task

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


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
 

 

Source
 

题意:

有n个机器,m个任务。每个机器至多能完成一个任务。对于每个机器,有一个最大运行时间xi和等级yi,对于每个任务,也有一个运行时间xj和等级yj。只有当xi>=xj且yi>=yj的时候,机器i才能完成任务j,并获得500*xj+2*yj金钱。问最多能完成几个任务,当出现多种情况时,输出获得金钱最多的情况。

分析:

  从一开头就知道了贪心的基本思路。就是先将任务和机器按降序排列(先时间,时间相同再按难度),然后采用贪心求解。当时脑洞太大,先选择机器,再选择该机器能完成的最大利润的任务,但是选择下一个机器的时候,忘了考虑被上一个机器忽略的那些任务,导致无限WA,到最后才想到有这一茬。网上给的题解是先选任务然后再选适合的最小机器。按照题解稍微修改了自己的代码然后AC了,其实和先选机器的思想是一致的,唉,太水无能。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 #define maxn 100010
 8 struct node{
 9     int tm, dif;
10 }MCH[maxn], TSK[maxn];
11 
12 bool cmp(node a, node b)
13 {
14     if(a.tm==b.tm) return a.dif > b.dif;
15     return a.tm > b.tm;
16 }
17 int main()
18 {
19     int n, m;
20     while(~scanf("%d%d", &n, &m))
21     {
22         for(int i = 1; i <= n; i++)
23             scanf("%d%d", &MCH[i].tm, &MCH[i].dif);
24         for(int i = 1; i <= m; i++)
25             scanf("%d%d", &TSK[i].tm, &TSK[i].dif);
26         sort(MCH+1, MCH+n+1, cmp);
27         sort(TSK+1, TSK+m+1, cmp);
28         int cnt = 0, c[102];
29         __int64 sum = 0;
30         memset(c, 0, sizeof(c));
31         for(int i = 1, j = 1; i <= m; i++)
32         {
33             while(j <= n && MCH[j].tm >= TSK[i].tm)
34             {
35                 c[MCH[j].dif]++;
36                 j++;
37             }
38             for(int k = TSK[i].dif; k <= 100; k++)
39             {
40                 if(c[k])
41                 {
42                     cnt++;
43                     c[k]--;
44                     sum += 500*TSK[i].tm+2*TSK[i].dif;
45                     break;
46                 }
47             }
48         }
49         printf("%d %I64d\n", cnt, sum);
50     }
51     return 0;
52 }

 

 
 
posted @ 2014-07-22 22:10  Sign_  阅读(617)  评论(0编辑  收藏  举报