码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4864 Task

时间:2014-07-22 23:51:17      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:acm   贪心   hdu   

题目链接:HDU 4864 Task

Task

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


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
 

Recommend
We have carefully selected several similar problems for you:  4871 4870 4869 4868 4867 
 

题意:

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

分析:

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

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

#define maxn 100010
struct node{
    int tm, dif;
}MCH[maxn], TSK[maxn];

bool cmp(node a, node b)
{
    if(a.tm==b.tm) return a.dif > b.dif;
    return a.tm > b.tm;
}
int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 1; i <= n; i++)
            scanf("%d%d", &MCH[i].tm, &MCH[i].dif);
        for(int i = 1; i <= m; i++)
            scanf("%d%d", &TSK[i].tm, &TSK[i].dif);
        sort(MCH+1, MCH+n+1, cmp);
        sort(TSK+1, TSK+m+1, cmp);
        int cnt = 0, c[102];
        __int64 sum = 0;
        memset(c, 0, sizeof(c));
        for(int i = 1, j = 1; i <= m; i++)
        {
            while(j <= n && MCH[j].tm >= TSK[i].tm)
            {
                c[MCH[j].dif]++;
                j++;
            }
            for(int k = TSK[i].dif; k <= 100; k++)
            {
                if(c[k])
                {
                    cnt++;
                    c[k]--;
                    sum += 500*TSK[i].tm+2*TSK[i].dif;
                    break;
                }
            }
        }
        printf("%d %I64d\n", cnt, sum);
    }
    return 0;
}




HDU 4864 Task

标签:acm   贪心   hdu   

原文地址:http://blog.csdn.net/u011439796/article/details/38051691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!