2014 Multi-University Training Contest 1 D(hdu 4864 经典贪心)

题目链接:

hdu 4864


题目输入输出:

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


题意:

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


思路:

将任务已x从大到小排序(x相同时已y从大到小排序)。然后也用相同排序方法排序机器。开始遍历任务,找出所有xi(xi>=xj),从中选择yi最小的一个作为这个任务的运行机器。500*xj+2*yj,所以yi可以当做次要因素,主观因素是时间。所以对任务和机器首先按时间大小,再按等级大小排序。


代码:

#include <stdio.h>
#include <algorithm>
#define Max 100002
using namespace std;
struct node{
    int x,y;
};
bool cmp(node a,node b)
{
    if(a.x==b.x)return a.y>b.y;
    else return a.x>b.x;
}
node mac[Max],tas[Max];
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        long long ans=0;
        int i,j,k;
        for(i=0;i<n;i++)
        {
            scanf("%d %d",&mac[i].x,&mac[i].y);
        }   
        for(i=0;i<m;i++)
        {
            scanf("%d %d",&tas[i].x,&tas[i].y);
        }
        sort(mac,mac+n,cmp);
        sort(tas,tas+m,cmp);
        int num[102]={0};             //用桶装十分巧妙 
        int cnt=0;
        for(i=0,j=0;i<m;i++)    
        {
            while(j<n&&mac[j].x>=tas[i].x)
            {
                num[mac[j].y]++;       //后面的加进来的任务必定可以被先前加进来的机器所完成 
                j++;
            }
            for(k=tas[i].y;k<=100;k++)
            {
                if(num[k])
                {
                    num[k]--;
                    cnt++;
                    ans+=500*tas[i].x+2*tas[i].y;
                    break;
                }
            }
        }
        printf("%d %lld\n",cnt,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值