hdu 4864 Task (贪心)

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
题意:公司里需要完成若干任务,每个任务有完成所需时间和任务等级,又有若干机器,每个机器有工作时间和等级,只有机器的工作时间和等级均大于等于任务的时间和等级才可完成该任务,每个机器每天只能完成一个任务,每个任务只能有一个机器单独完成,每完成一个任务公司得到的利润为:500*任务时间+2*任务等级;求一天内公司所能获得的最大利润; 利用贪心思想,先把机器和任务按时间递减排序,时间相同按等级递减排序;然后遍历每个任务,找到能完成该任务的所有机器,使用等级最小的机器;比如现在有个100min,1等级的,可用机器等级有1,2,3;如果选择等级为3的机器,后边出现一个等级为3的任务,就无法完成;


看看代码:


#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#define INF 0x3f3f3f3f

using namespace std;

struct IN{
    int t, level;
}mach[100005],task[100005];
int M, N;
bool cmp(IN a, IN b){
    if(a.t!=b.t) return a.t > b.t;
    return a.level > b.level;
}
int main(){
    while(cin >> N >> M){
        for(int i=0; i<N; i++)
            cin >> mach[i].t >> mach[i].level;
        for(int i=0; i<M; i++)
            cin >> task[i].t >> task[i].level;
        sort(mach, mach+N, cmp);
        sort(task, task+M, cmp);
        int cnt[105];                                //cnt用来记忆每个等级的机器的数量;
        memset(cnt, 0, sizeof(cnt));
        long long num=0, ans=0;
        for(int i=0, j=0; i<M; i++){                  //i表示第i个任务,j表示第j台机器;
            while(j<N && task[i].t<=mach[j].t){       //j不能大于N,机器时间>=任务时间
                cnt[mach[j].level]++;                 //满足条件的机器所属等级的机器数量;
                j++;                                  //下一台机器;
            }
            for(int k=task[i].level; k<=100; k++){     
                if(cnt[k]){                             //k等级的机器数量不为零,说明有机器可完成i工作;
                    cnt[k]--;                           //将该机器pop掉,因为每个机器只能完成一个任务;
                    num++;
                    ans+=(500*task[i].t+2*task[i].level);
                    break;
                }
            }
        }
        cout << num << ' ' << ans << endl;
    }
    return 0;
}
有个需要注意的地方是一开始我的排序使用sqort,WA了,改成sort后就AC了;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值